JSP execution process:

1 The client sends a request.

The Web container translates JSPS into Servlet source code.

The Web container compiles the resulting source code.

4 The Web container loads the compiled code and executes it.

5 Send the execution result to the client.

So JSP is essentially a servlet. We can look at the Java files converted by JSP.

/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/8.5.43 * Generated at: 2019-08-12 06:31:53 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class jsp2_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private static final java.util.Set<java.lang.String> _jspx_imports_packages; private static final java.util.Set<java.lang.String> _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add("javax.servlet"); _jspx_imports_packages.add("javax.servlet.http"); _jspx_imports_packages.add("javax.servlet.jsp"); _jspx_imports_classes = null; } private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public java.util.Set<java.lang.String> getPackageImports() { return _jspx_imports_packages; } public java.util.Set<java.lang.String> getClassImports() { return _jspx_imports_classes; } public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory; } public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { if (_jsp_instancemanager == null) { synchronized (this) { if (_jsp_instancemanager == null) { _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return _jsp_instancemanager; } public void _jspInit() { } public void _jspDestroy() { } public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { final java.lang.String _jspx_method = request.getMethod(); if (!" GET".equals(_jspx_method) && !" POST".equals(_jspx_method) && !" HEAD".equals(_jspx_method) && ! javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD"); return; } final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html; charset=UTF-8"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write(" <title>Title</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); Out. Write (" < p > I'm jsp2 < / p > \ r \ n "); out.write("</body>\r\n"); out.write("</html>\r\n"); } catch (java.lang.Throwable t) { if (! (t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out ! = null && out.getBufferSize() ! = 0) try { if (response.isCommitted()) { out.flush(); } else { out.clearBuffer(); } } catch (java.io.IOException e) {} if (_jspx_page_context ! = null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); }}}Copy the code

The familiar _jspService is the method that handles the request.

First of all, something that’s very, very rarely used in development. Write Java code in JSP..

// Here you can write code just like in class. <% int I = 1 + 3; System.out.println(i); % >Copy the code
// Output a variable to the page <%= I %>Copy the code
Declare a member variable <%! int x = 20; % >Copy the code

Let’s take a look at the code from JSP to Java.

This is the member variable that we declared

These are the Java code we wrote and the variables we output to the page

 

Mention JSP has to say is: three instructions, four fields, nine built-in objects (including four fields) at first sound a lot of scary. In fact, those of you who have used servlets already know a lot of this.(Of course, no one uses native servlets these days, if not for interviews. I don’t think I’ll be watching this either.

First, what is a built-in object? It literally means an object that you can use without using new. Why don’t we use new when we want to use objects? The reason is simple: The JSPS we write are actually servlets, and the code is in a Service, so the nine built-in objects must also be in a Service

Go to the source code of the service. Check it out. Or you can just look at the image below

Let’s introduce them one by one

1 out: Servlet has response.getwriter ().print(); Method to input content to the page, JSP output using out. A complete JSP page is converted to HTML using the out.write() method to output data and HTML tags as strings to the client

2 config: When we use servlets, we can also configure initialization parameters of servlets when we configure a servlet and servletMapping in web.xml. You can use this.getinitParameter (“xx”) in the servlet; To get the servlet initialization parameters, in JSP we can also use config to get the initialization parameters.

3 Page: Actually this

4 Response: Yes this is the same as the response in the servlet.

 

5 exception: I have to say this. To use this built-in object, the modified page must be declared an Exception page. The JSP directive will say how to declare the exception page

6 Request: One of the domain objects like Request in a servlet (present in a lifecycle request)

Session: One of the domain objects as in the servlet (exists in a session without closing the browser)

Application: one of the domain objects corresponds to the servletContext in the servlet.(Life cycle is born when the server is started and dies when the server is shut down.)

PageContext: Last built-in object, last domain object. The scope of this domain object is on the current page. Set setAttribute(“key”,”value”) on page A; On page B getAttribute(“key”); You can’t get it, you can only use it on his own page, and it doesn’t seem to work. However, existence is reasonable. This domain object does much more than that. First he can get the other eight built-in objects.

 

No dick. If you can’t remember nine built-in objects then just remember this one. Then you memorize 8 methods. Ah-ha-ha.

Second, it can manipulate the contents of four domain objects with one object.

<% // Set pagecontext.setAttribute ("pkey", "pvalue"); request.setAttribute("rkey", "rvalue"); session.setAttribute("skey", "svalue"); application.setAttribute("akey", "avalue"); Pagecontext.getattribute ("pkey", 1); pageContext.getAttribute("rkey", 1); pageContext.getAttribute("rkey", 1); pageContext.getAttribute("rkey", 1); / / query specifies the key, the search order is pageContext - > request - > session - > application to find which domain pageContext. Where there is returned findAttribute (" rkey "); // Delete the values in the domain object. Delete which domain has which!! pageContext.removeAttribute("rkey"); % >Copy the code

Three orders

1 Page (following common configurations)

<%@ page contentType=“”text/html; Charset =UTF-8″ %> Specifies the file encoding and corresponding encoding for the client

<% @page ‘ ‘import’ = ‘”java.util.List” %

<% @page errorPage="xxx. JSP "%> Specifies which page to go to if the current page fails

<% @page isErrorPage= ‘ ‘”true” %> Set the current page to errorPage Note! Only when this directive is set will the JSP page have an exception built-in object.

2 include

<% @include file="xxx.jsp"%> Static include. Using this directive anywhere on the page includes the file it points to on the page.

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> title </title> </head> <body> Jsp3 <% @include file="jsp4.jsp" %> </body> </html>Copy the code
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> title </title> </head> <body>Copy the code

Note that this static inclusion does not necessarily generate JSP4 Java and class files

 

 

Instead, it compiled a Java file that cloned the entire contents of JSP4.

There’s a dynamic inclusion that’s the opposite of that

The way to use it is

<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title> title </title> </head> <body> I am jsp3 < JSP :include page="jsp4.jsp"></jsp:include> </body> </html>Copy the code

The page displays the same result. But the execution process is really different.

You can see that the JSP4 files are also generated at this point

 

 

 

 

In JSP3, the method is called to query the contents of JSP4.

This is the difference between dynamic inclusion and static inclusion.

3 the taglib directive

<% @taglib url=” tag library URL” prefix=” namespace used in JSP “%>

This directive is used to import the tripartite label library. About the use of tag libraries. In the next article

That’s a brief introduction to JSP.