Many people will think that the performance of JSP is much different from the servlet, in fact, the performance difference is only the first time execution. Since the JSP is executed for the first time, it will be compiled into the servlet's class file, the. class, and when repeated calls are executed, the servlet generated for the first time is executed without re-compiling the JSP into Servelt.
So, in addition to the first compilation, it takes a long time for the JSP and servlet to execute almost the same speed. The Web container processing JSP file request execution process mainly includes the following 4 parts:
1. The client sends request requests
2.JSP Container translate JSP into servlet source code
3. The resulting servlet source code is compiled and loaded into memory execution
4. Response the result (response) to the client
When executing a JSP Web page, it can usually be divided into two periods: the translation period (translation time) and the requested period (request time).
Translation period: The JSP Web page is transferred into a servlet class.
Request period: After the Servlet class executes, the response results to the client.
Two things were done during the translation:
Translation period: The JSP Web page is transferred to the servlet source code. java.
Compile time: The servlet source code. Java is compiled into a servlet class. class.
When the JSP Web page is executed, JSP container will do the check work, if the JSP page is found to have updated changes, JSP container will compile JSP into a servlet again; If the JSP is not updated, it executes the servlet generated earlier.
- (showdate.jsp)
- <%@ page language="java" contenttype="text/html;charset=gb2312" import= "java.text.*, java.util.*; " %>
- <html>
- <head>
- <title>show time</title>
- </head>
- <body>
- Hello:
- <%
- SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");
- String str = Format.format (new Date ());
- %>
- <%=str%>
- </body>
- </html>
When SHOWDATE.JSP is deployed, start the Tomcat server.
1. Enter the configured path in Internet Explorer .... showdate.jsp request this page.
2.JSP Container that the Tomcat server translates showdate.jsp into Showdate_jsp.java source files.
3. Compile the Showdate_jsp.java source file into Showdate_jsp.class at the same time.
4. Compile and execute the Showdate_jsp.class class, process the request, return the response, and the container will return the generated page to the client display.
- (Transferred to Java source file Showdate_jsp.java)
- Package org.apache.jsp.ch04;
- Import javax.servlet.*;
- Import javax.servlet.http.*;
- Import javax.servlet.jsp.*;
- Import java.text.*;
- import java.util.*;;
- Public final class Showdate_jsp extends Org.apache.jasper.runtime.HttpJspBase
- Implements Org.apache.jasper.runtime.JspSourceDependent {
- private static Java.util.List _jspx_dependants;
- Public Object getdependants () {
- return _jspx_dependants;
- }
- public void _jspservice (HttpServletRequest request, httpservletresponse response)
- Throws Java.io.IOException, Servletexception {
- Jspfactory _jspxfactory = null;
- PageContext PageContext = null;
- HttpSession session = null;
- ServletContext application = null;
- ServletConfig config = null;
- JspWriter out = null;
- Object page = this ;
- JspWriter _jspx_out = null;
- PageContext _jspx_page_context = null;
- try {
- _jspxfactory = jspfactory.getdefaultfactory ();
- Response.setcontenttype ("text/html; charset=gb2312 ");
- 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>show time</title>\r\n");
- Out.write ("</head>\r\n");
- Out.write ("<body> \ r \ n");
- Out.write ("\thello: \ r \ n");
- Out.write ("\ t");
- SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");
- String str = Format.format (new Date ());
- Out.write ("\ r \ n");
- Out.write ("\ t");
- Out.print (str);
- Out.write ("\ r \ n");
- Out.write ("</body>\r\n");
- Out.write ("</html>");
- } catch (Throwable t) {
- if (! ( T instanceof Skippageexception)) {
- Out = _jspx_out;
- if (out! = null && out.getbuffersize ()! = 0)
- Out.clearbuffer ();
- if (_jspx_page_context! = null) _jspx_page_context.handlepageexception (t);
- }
- } finally {
- if (_jspxfactory! = null) _jspxfactory.releasepagecontext (_jspx_page_context);
- }
- }
- }
When a JSP page is translated into a servlet, the content consists of three parts:
- public void _jspinit () {..}
- --When the JSP Web page starts executing, this method is executed first, performing the initialization work
- public void _jspdestory () {...}–jsp Web page last executed method
- public void _jspservice (HttpServletRequest request, httpservletresponse response)
- Throws Java.io.IOException, Servletexception {
The most important programs in the JSP Web page are executed here, making a simple comparison between showdate.jsp and Showdate_jsp.java:
Part I: Comparison of page properties
- <%@ page language="java" contenttype="text/html;charset=gb2312"%>
- Response.setcontenttype ("text/html; charset=gb2312 ");
- Returning the client's page properties through the response response setting
Part Two: HTML tags
- <html>
- <head>
- <title>show time</title>
- </head>
- ..
- </html>
- Out.write ("\ r \ n");
- Out.write ("<HTML>\r\n");
- Out.write ("<head>\r\n");
- Out.write ("<title>show time</title>\r\n");
- Out.write ("</head>\r\n");
- Out.write ("<body> \ r \ n");
- Out.write ("\thello: \ r \ n");
- Out.write ("\ t");
- To write HTML tags to the client through an Out object
Part III: Declared objects
- <%
- SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");
- String str = Format.format (new Date ());
- %>
Local variables declared in the _jspservice method:
- SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");
- String str = Format.format (new Date ());
Part IV: Expressions
- <%=str%>
- Out.print (str); Write to print the value of the STR variable
JSP translates into servlet verbose process