I believe everyone is familiar with this page. May I say this is not a Servlet? We can understand it as a dynamic Servlet. Among them, _ jspInit, _ jspService, and _ jspDestroy correspond to init, service, and destroy in Servlet respectively. They have the same functions, its advantages are obvious, so we don't need to write the above Code in Servlet. This accelerates the development efficiency. In this way, we can easily understand the jsp generation process. For example
Next, let's analyze the differences between Servlet and Jsp.
1: Jsp needs to be compiled into Servlet for the first time (only once)
2: Jsp is a Java and HTML file that can be combined into a. jsp file, and Servlet is directly separated from Html.
3: different focuses. SP focuses on views. Servlet is mainly used for control logic.
3: JSP syntax 3.1: command element Command element: it is mainly used to provide information about the entire jsp page during the conversion phase, the command element does not generate any output to the current output stream (to put it bluntly, it defines some JSP-related information)
3.1.1: page command The page command acts on the entire jsp page and defines many page-related attributes. Let's take a look at these attributes.
This code is often generated on the jsp page.<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
The preceding attributes are the directive elements.Language indicates the development language,ContentType indicates the text type,Charset andPageEncodingIndicates the encoding format.
After converting jsp into Servlet, we can find it in the _ jspService method.Response. setContentType ("text/html; charset = UTF-8"); then let's look at other directive Elements
<% @ Page import = "" %> This is the class for importing java and the function of referencing classes in development is the same, the Servlet generated after this command element is added will be imported into this class.
<% @ Page isELIgnored = "true" %> many parameters are used to determine whether to enable EL expressions. If the expression is set to Servlet2.3 or earlier, the default value is true. If the expression is set to 2.4 or later, the default value is false.
<% @ Page session = "true" %> whether to start the Session. If it is false, the Session in this jsp is invalid. The default value is true.
<% @ Page errorPage = "" %> the error page pointed to after an error occurs. In this case, the web. xml configuration is invalid.
<% @ Page isErrorPage = "false" %> specifies whether the JSP is an error page of another page. The default value is false.
<% @ Page buffer = "8kb" %> specify the size of the buffer used by the out object. The default value is 8 KB, and none means no buffer is set.
Of course, there are some command elements that you can refer to. Remember that the scope of these command elements is only the current page.
3.1.2: include command From the literal meaning, we know that include is the meaning of include. So what is the meaning of this directive element? It is mainly used to include a static file in the JSP page. This file can be an html file, a jsp page, a text file, and a piece of java code. Let's take a look at the running results.
First write a simple jsp page
1
Then we reference it in index. jsp.
1 <%@ include file="myinclude.jsp" %>
The running result is as follows:
If you want a piece of code, modify myinclede. jsp to the following:
1 <% @ page import = "java. util. * "%> 2
The running result here is as follows:
3.1.3: taglib command
This command is used to customize tags.
Tablib has three attributes: uri, tagdir, and prefi, which will be described in detail in future custom tags (for a moment)
3.2: script Element
The script element consists of three parts: Declaration, script segment, and expression.
3.2.1: Declaration
This is the same as declaring a variable or method in java code. Let's look at its writing method.
<%! Int I = 1; %>
<% Out. print (I); %>
After the Declaration, we can directly use this variable. Remember to add the declaration after %!
Of course, you can also declare a method as follows:
<%! public String getUserName(){return "zhangsan";} %><% out.print(getUserName());%>
Note: Use <%! %> When the declared variables are converted into Servlet classes in the jsp Container, the instance variables or class variables of the class are added (static keywords are added during Declaration). Therefore, pay attention to thread security issues 3.2.2: script segment.
The script segment is the code in the java code <%> to be executed during request processing. This is not much to say <% out. print (getUserName (); %>.
3.2.3: Expression
The expression script element is an expression completed in java. When a request computes these complete expressions, the result is converted to a string and inserted into the current input stream. The expression ends with <% = start %>.
Name: <% = "zhangsan" %> This is an expression (you do not need to add any symbols later)
3.3: Action Element
Unlike JSP command elements, JSP action elements work in the request processing stage. JSP action elements are written in XML syntax. There are mainly 20 standard jsp action elements. Below I will mainly discuss them in common use.
1: <jsp: useBean>, <jsp: setProperty>, <jsp: getProperty>
<Jsp: useBean> an action is used to load a JavaBean that will be used on the JSP page. Its attributes include the id of the object to a name, and the class specifies the full package name of the Bean, scope indicates the range of the object.
<Jsp: setProperty> mainly sets the object value, where name is the bean object name,
<Jsp: getProperty> mainly used to obtain objects. Let's take a look at the example below.
public class UserBean { private String userName; private String realName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; }
<Body> <jsp: useBean id = "user" class = "com. lp. beans. userBean "scope =" session "> </jsp: useBean> <jsp: setProperty property =" userName "name =" user "value =" zhangsan "/> <jsp: setProperty property = "realName" name = "user" value = "Zhang San"/> userName: <jsp: getProperty property = "userName" name = "user"/> real name: <jsp: getProperty property = "realName" name = "user"/> </body>
Running result:
2: <jsp: forward page = ""> </jsp: forward> This is the same as the previous forward.
3: <jsp: include page = ""> </jsp: include> contains pages.
I will not mention anything else here. I don't need to use this estimate for development now. You can take a look at it for yourself.
4: Nine built-in jsp objects
In Servlet, do we see many objects in Jsp, such as Request and response? We have said that Jsp is a dynamic Servlet. Of course, we will look at the built-in objects in Jsp 9.
4.1: request
4.2: response
4.3: out. The out object is JspWriter.
4.4: page is the Instance object page after jsp is converted to Servlet = this;
4.5: pageContext to get all attributes of the current page
4.6: application is an instance of javax. servlet. ServletContext.
4.7: config is a javax. servlet. ServletConfig instance.
4.8: session
4.9: exception indicates exception handling.
We should understand this at first glance.
5. Objects and ranges
Jsp4 range
5.1: request: Valid only once in a request
5.2: the page is valid only on the current page.
5.3: the Session is valid throughout the Session.
5.4: The application is valid throughout the application, knowing that the server is disabled