Detailed process of converting JSP into Servlet, and converting jsp into servlet

Source: Internet
Author: User

Detailed process of converting JSP into Servlet, and converting jsp into servlet

Many people think that the execution performance of JSP is much different from that of Servlet. In fact, the difference in execution performance is only the first execution. This is because after the first execution of JSP, it will be compiled into a Servlet class file, that is. class. When the call is executed again, the Servlet generated for the first time will be executed directly, instead of re-compiling the JSP into Servelt.

Therefore, the execution speed of JSP and Servlet is almost the same except that the first compilation takes a long time. The execution process of JSP file requests processed by Web containers mainly includes the following four parts:

1. The client sends a Request

2. JSP Container translates JSP into Servlet Source Code

3. Compile the generated Servlet Source Code and load it To the memory for execution.

4. Send the Response to the client.

When executing a JSP page, it can be divided into two periods: Translation Time and Request Time ).

◆ Translation period: JSP web pages are transferred to Servlet classes.

◆ Request period: After the Servlet class is executed, the response is sent to the client.

During the translation, two things were done:

◆ Translation period: Transfers JSP pages to Servlet Source Code. java.

◆ Compilation period: Compile Servlet Source Code. java into Servlet class. class.

When a JSP webpage is executed, the JSP Container performs a check. If an update or modification is found on the JSP webpage, the JSP Container will compile the JSP into a Servlet again. If the JSP is not updated, directly execute the Servlet generated above.

  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>

Start the Tomcat server after showdate. jsp is deployed.

1. Enter the configured path... showdate. jsp In the IE browser to request this page.

2. JSP Container: the Tomcat server translates showdate. jsp into the showdate_jsp.java source file.

3. Compile the showdate_jsp.java source file into showdate_jsp.class.

4. Compile and execute the showdate_jsp.class class to process requests and return responses. The Container returns the generated page to the client.

  1. (The transferred 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. getdefafactory Factory ();
  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 ("
  38. Out. write ("
  39. Out. write ("<title> Show time </title> \ r \ n ");
  40. Out. write ("
  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 ("
  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 the JSP page is translated into Servlet, the content mainly includes three parts:

  1. Public void _ jspInit (){..}
  2. -- When the JSP page is executed at the beginning, this method is first executed to execute initialization.
  3. Public void _ jspDestory () {...}-Method for executing the final JSP webpage
  4. Public void _ jspService (HttpServletRequest request, HttpServletResponse response)
  5. Throws java. io. IOException, ServletException {

The most important programs on the JSP page are executed here. Make a simple comparison between showdate. jsp and showdate_jsp.java:

Part 1: Comparison of page properties

  1. <% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
  2. Response. setContentType ("text/html; charset = gb2312 ");
  3. // Set the page attribute of the returned client through the response

Part 2: HTML tags

  1. <Html>
  2. <Head>
  3. <Title> Show time </title>
  4. </Head>
  5. ..
  6. </Html>
  7.  
  8. Out. write ("\ r \ n ");
  9. Out. write ("
  10. Out. write ("
  11. Out. write ("<title> Show time </title> \ r \ n ");
  12. Out. write ("
  13. Out. write ("<body> \ r \ n ");
  14. Out. write ("\ tHello: \ r \ n ");
  15. Out. write ("\ t ");
  16. // Write HTML tags to the client through the out object

Part 3: declared objects

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

The local variables declared in the _ jspService method:

  1. SimpleDateFormat format = new SimpleDateFormat ("yyyy/MM/dd ");
  2. String str = format. format (new Date ());

Part 4: Expressions

  1. <% = Str %>
  2. Out. print (str); // write to print the value of the str variable.

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.