JSP translates into servlet verbose process

Source: Internet
Author: User

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.

  1. (showdate.jsp)
  2. <%@ page language="java" contenttype="text/html;charset=gb2312" import= "java.text.*, java.util.*; " %>
  3. <html>
  4. <head>
  5. <title>show time</title>
  6. </head>
  7. <body>
  8. Hello:
  9. <%
  10. SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");
  11. String str = Format.format (new Date ());
  12. %>
  13. <%=str%>
  14. </body>
  15. </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.

  1. (Transferred to Java source file Showdate_jsp.java)
  2. Package org.apache.jsp.ch04;
  3. Import javax.servlet.*;
  4. Import javax.servlet.http.*;
  5. Import javax.servlet.jsp.*;
  6. Import java.text.*;
  7. import java.util.*;;
  8. Public final class Showdate_jsp extends Org.apache.jasper.runtime.HttpJspBase
  9. Implements Org.apache.jasper.runtime.JspSourceDependent {
  10. private static Java.util.List _jspx_dependants;
  11. Public Object getdependants () {
  12. return _jspx_dependants;
  13. }
  14. public void _jspservice (HttpServletRequest request, httpservletresponse response)
  15. Throws Java.io.IOException, Servletexception {
  16. Jspfactory _jspxfactory = null;
  17. PageContext PageContext = null;
  18. HttpSession session = null;
  19. ServletContext application = null;
  20. ServletConfig config = null;
  21. JspWriter out = null;
  22. Object page = this ;
  23. JspWriter _jspx_out = null;
  24. PageContext _jspx_page_context = null;
  25. try {
  26. _jspxfactory = jspfactory.getdefaultfactory ();
  27. Response.setcontenttype ("text/html;       charset=gb2312 ");
  28. PageContext = _jspxfactory.getpagecontext (this, request, response,
  29. NULL, True, 8192, true);
  30. _jspx_page_context = PageContext;
  31. application = pagecontext.getservletcontext ();
  32. config = pagecontext.getservletconfig ();
  33. session = pagecontext.getsession ();
  34. out = pagecontext.getout ();
  35. _jspx_out = out;
  36. Out.write ("\ r \ n");
  37. Out.write ("<HTML>\r\n");
  38. Out.write ("<head>\r\n");
  39. Out.write ("<title>show time</title>\r\n");
  40. Out.write ("</head>\r\n");
  41. Out.write ("<body> \ r \ n");
  42. Out.write ("\thello: \ r \ n");
  43. Out.write ("\ t");
  44. SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");
  45. String str = Format.format (new Date ());
  46. Out.write ("\ r \ n");
  47. Out.write ("\ t");
  48. Out.print (str);
  49. Out.write ("\ r \ n");
  50. Out.write ("</body>\r\n");
  51. Out.write ("</html>");
  52. } catch (Throwable t) {
  53. if (! ( T instanceof Skippageexception)) {
  54. Out = _jspx_out;
  55. if (out! = null && out.getbuffersize ()! = 0)
  56. Out.clearbuffer ();
  57. if (_jspx_page_context! = null) _jspx_page_context.handlepageexception (t);
  58. }
  59. } finally {
  60. if (_jspxfactory! = null) _jspxfactory.releasepagecontext (_jspx_page_context);
  61. }
  62. }
  63. }

When a JSP page is translated into a servlet, the content consists of three parts:

    1. public void _jspinit () {..}
    2. --When the JSP Web page starts executing, this method is executed first, performing the initialization work
    3. public void _jspdestory () {...}–jsp Web page last executed method
    4. public void _jspservice (HttpServletRequest request, httpservletresponse response)
    5. 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

    1. <%@ page language="java" contenttype="text/html;charset=gb2312"%>
    2. Response.setcontenttype ("text/html;     charset=gb2312 ");
    3. Returning the client's page properties through the response response setting

Part Two: HTML tags

  1. <html>
  2. <head>
  3. <title>show time</title>
  4. </head>
  5. ..
  6. </html>
  7. Out.write ("\ r \ n");
  8. Out.write ("<HTML>\r\n");
  9. Out.write ("<head>\r\n");
  10. Out.write ("<title>show time</title>\r\n");
  11. Out.write ("</head>\r\n");
  12. Out.write ("<body> \ r \ n");
  13. Out.write ("\thello: \ r \ n");
  14. Out.write ("\ t");
  15. To write HTML tags to the client through an Out object

Part III: Declared objects

    1. <%
    2. SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");
    3. String str = Format.format (new Date ());
    4. %>

Local variables declared in the _jspservice method:

    1. SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");
    2. String str = Format.format (new Date ());

Part IV: Expressions

    1. <%=str%>
    2. Out.print (str); Write to print the value of the STR variable

JSP translates into servlet verbose process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.