JSP running principle and running process

Source: Internet
Author: User

Learning J2EE, a preliminary understanding of JSP, wrote a small summary: J2EE-JSP, was not very familiar with its operating mechanism. When I learned about DRP, I got started with JSP again. On the basis of my previous understanding, I learned how it works.
1,JSPOperating principle

(1) When the WEB Container JSP page access request is sent, it will send the access request to the JSP Engine for processing. The JSP Engine in Tomcat is a Servlet program, which is responsible for interpreting and executing JSP pages.

(2) When each JSP page is accessed for the first time, the JSP Engine first translates it into a Servlet Source program, and then compiles the Servlet Source program into a Servlet class file, then, the WEB container loads and interprets the Servlet program translated from the JSP page in the same way as calling the common Servlet program.

(3) Tomcat 5 places the Servlet Source file and class file created for the JSP page in "apache-tomcat-5.5.26 \ work \ Catalina \ localhost \ <应用程序名> \ "Directory, Tomcat translates the JSP page into a Servlet package named org. apache. jsp (under the apache-tomcat-5.5.26 \ work \ Catalina \ localhost \ org \ apache \ jsp \ file)

Example:

HelloJSP. jsp:(File directory: D: \ apache-tomcat-5.5.26 \ webapps \ test_jsp \ HelloJSP. jsp)

<% @ Pagelanguage = "java" import = "java. util. *" pageEncoding = "gb2312" %>           Simple JSP page exampleThis is a simple JSP page example-HelloJSP

When the HelloJSP. jsp page is requested, the JSP compiler in the Web server will compile and generate the corresponding Java file, as shown below:


HelloJSP_jsp. Java:(File directory: D: \ apache-tomcat-5.5.26 \ work \ Catalina \ localhost \ test_jsp \ org \ apache \ jsp \ HelloJSP_jsp.java)

Package org. apache. jsp; import javax. servlet. *; import javax. servlet. http. *; import javax. servlet. jsp. *; import java. util. *; public final class HelloJSP_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. getdefafactory Factory (); 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 ("\ R \ n "); out. write (" \ t\ R \ n "); out. write (" \ tSimple JSP page example\ R \ n "); out. write (" \ t\ R \ n "); out. write ("\ R \ n "); out. write (" \ t this is a simple JSP page example-HelloJSP
\ R \ n "); out. write (" \ t\ R \ n "); out. write ("\ R \ n ");} 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 );}}}


The above code is HelloJSP. java code compiled by jsp. Its path (my computer) is: D: \ apache-tomcat-5.5.26 \ work \ Catalina \ localhost \ test_jsp \ org \ apache \ jsp (test_jsp is my project name ). There will be two corresponding files in this directory, one is the class file and the other is the java

The file is as follows:



2, <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Signature + signature/Signature + PC9oMT4KPHA + signature/Signature + signature/Nu6e2y7eis/bH68fzoaM8L3A + signature + rXE1LS0 + signature + 8/s06bWwb/Signature = "http://www.2cto.com/uploadfile/Collfiles/20140417/20140417092148152.jpg "alt =" ">


3. Summary JSP execution process:

1) First, the client sends a request to access the JSP webpage.

2) then, The. JSP file to be accessed by the JSP iner is translated into the Servlet Source Code (. java file)

3) then, compile the source code (. java file) of the generated Servlet, generate the. class file, and load it To the memory for execution.

4) Finally, send the response to the client.

When executing a JSP webpage file, it takes two periods: Translation Time and Request Time ).

Conversion period: JSP is translated into Servlet class (. class file ).

Request period: After the Servlet class (. class file) is executed, the response is sent to the client.


During the translation process, there are two main tasks:

(1) Translate JSP pages into Servlet Source Code (. java). This segment is called the Translation time );

(2) Compile the Servlet Source Code (. java) into a Servlet class (. class). This stage is called the Compilation time ).

In fact, JSP is a Servlet.



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.