I. Introduction
What is JSP? It
is Java Server Pages: Java server-side pages, also like servlets, develop dynamic web technology
The greatest feature:
- Writing JSP code is like writing HTML code
the difference:
- HTML only provides static data to users
- Java code can be embedded in JSP pages to provide users with dynamic data
Second, analyze the principle of JSP by reading the original code
Idea: How does JSP execute?
- There is no problem at the code level
- Internal work of the server
tomcat中有一个work目录,IDEA中使用Tomcat的会在IDEA的Tomcat中生产一个work目录
The browser sends a request to the server. No matter what resource is accessed, it is actually accessing a Servlet.
JSP will eventually be converted to a Java class. JSP is essentially a Servlet!
- Judgment request
- Some built-in objects
- Code added before output page
- The above objects can be used directly in the JSP page
The flow chart of accessing the jsp page is as follows:
Let's start with a piece of JSP code:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String name = "徐志斌";
%>
name:<%=name%>
</body>
</html>
In the JSP page, as long as it is java code, it will be output intact .
If it is HTML code, it will be converted to: out.write("name:") and output to the front-end page
Three, JSP basic grammar (emphasis)
Any language has its own language. As an application of Java technology, JSP has some of its own expanded grammars (you just need to understand it), and all java grammars are supported.
1. JSP expression :
<%--jsp表达式
作用:用来将程序的输出,输出到客户端
<%= 变量或者表达式%>
--%>
<%= new java.util.Date()%>
2. JSP script fragment :
<%--jsp脚本片段--%>
<%
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
out.println("<h1>Sum = " + sum + "</h1>");
%>
Reimplementation of JSP script fragments:
<%
int x = 10;
out.println(x);
%>
<p>这是一个jsp文档</p>
<%
int y = 20;
out.println(y);
%>
<hr>
<%--再代码中,嵌入HTML元素--%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>Hello World <%=i%> </h1>
<%
}
%>
3. JSP statement :
<%!
static{
System.out.println("Loading");
}
private int globalVar = 0;
public void jspInit(){
System.out.println("进入了方法!");
}
%>
jsp statement: will be compiled into the java class generated by jsp, and the others will be generated into the _jspService method
Four, JSP instructions (understand)
<%@page args...%>
<%@include file=""%>
@include会将两个页面合二为一
<%@include file="common/header.jsp"%>
<h1>网页主体</h1>
<%@include file="common/footer.jsp"%>
jsp标签:
jsp:include:拼接页面,本质还是三个
<jsp:include page="/common/header.jsp"/>
<h1>网页主体</h1>
<jsp:include page="/common/footer.jsp"/>
Five, nine built-in objects
- PageContext can store data
- Request can store data
- Response
- Session can store data
- Application (ServletContext) can store data
- config (ServletConfig)
- out
- page is basically unused
- exception
// 内置对象,存储数据
pageContext.setAttribute("name1","徐志斌1号"); //保存的数据只在一个页面中有效
request.setAttribute("name2","徐志斌2号"); //保存的数据只在一次请求中有效,请求转发会携带这个参数
session.setAttribute("name3","徐志斌3号"); //保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","徐志斌4号"); //保存的数据只在服务器中有效,从打开服务器到关闭服务器
request : The client sends a request to the server, and the generated data is useless after the user reads it. For example: news, users are useless after reading
session : The client sends a request to the server, and the generated data is useless after the user reads it. For example: shopping cart
application : The client sends a request to the server, and the generated data is used up by the user, and other users may still use it. For example: chat data