JSP execution Process detailed

Source: Internet
Author: User
Tags apache tomcat

Review the concept of JSP

JSP is the abbreviation of Java Server page, and the JSP tag and Java program fragment are included in the traditional HTML page.

Basic syntax for JSPs: Two annotation types, three script elements, three element directives, and eight action instructions.

JSP's built-in objects are commonly used: Request, Response, out, Session, cookie, application and so on. Local variables and global variables in JSPs

There is a small example in the JSP Basic Grammar blog post counter.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

The purpose of this example is to differentiate between the <%! Variables defined in%> and <%%>:

<%! Variables and methods within a%> are variables and methods within a class, which is the member variable and member method of the page, and each time a user accesses this page, Count adds one.

A variable within a <%%> is a variable of a method that is a local variable, regardless of how many times the page is accessed, the value of i++ is always 0.

We see the difference between the two variables, but don't understand why this is different. Here is a detailed explanation. JSP execution Process diagram

First time Request:

When a JSP page on the server is executed on the first request, the JSP engine on the server translates the JSP paging file into a. java file, the servlet, and compiles the Java file to generate the. class bytecode file, and then executes the bytecode file in response to the client's request.

Request again:

The JSP engine executes the bytecode file directly in response to the customer.

Servlet translated by JSP

We can view the Servelt translated by JSP to deepen the understanding of multiple JSPs. The directory where the JSP translates into SERVLT is as follows:

The names of these servlets end with _jsp.java. Here's what's Counter_jsp.java:

/* * Generated by the Jasper component of Apache Tomcat * Version:apache tomcat/7.0.59 * Generated at:2015-08-07 05:13:1       3 UTC * Note:the Last modified time of this file is 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.*;import Java.util.*;p ublic Final class Counter_jsp extends Org.apache.jasper.runtime.HttpJspBase implements Org.apache.jasper . Runtime.    jspsourcedependent {int count = 0;            synchronized void SetCount () {count++;          }; 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 Javax.el.ExpressionFactory _el_expressionfactory;  Private Org.apache.tomcat.InstanceManager _jsp_instancemanager; Public java.util.map&Lt;java.lang.string,java.lang.long> getdependants () {return _jspx_dependants; } public void _jspinit () {_el_expressionfactory = _jspxfactory.getjspapplicationcontext (Getservletconfig (). GetServle    Tcontext ()). Getexpressionfactory ();  _jsp_instancemanager = Org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager (Getservletconfig ()); } public void _jspdestroy () {} public void _jspservice (final javax.servlet.http.HttpServletRequest request, final Java X.servlet.http.httpservletresponse response) throws Java.io.IOException, Javax.servlet.ServletException {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 ("<!      DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >\r\n ");      Out.write ("

Perhaps you answer the question, Counter_jsp.java did not inherit HttpServlet, why call them servlet? Please note the following

In the Apache-tomcat-7.0.59\java\org\apache\jasper\runtime directory to save httpjspbase This class of source code files,

Because of the following relationships, these _jsp.java files are servlets.

HttpServlet     successor Httpjspbase         

Back in Counter_jsp.java, we can apply the Notepad lookup function to find the variables we defined on the JSP page. can see:

<%! The variables and methods defined in%> are member variables and member methods of the class, and are global variables.

The variable defined in <%%> is a local variable in the _jspservice () {} method. Internal methods of JSP

_jspinit () {}:jsp page is initialized when the method is called, and the method is only executed once at initialization time, so you can do some initialization of the parameter configuration, such as one-time work, created by the author

_jspdestroy () {}:jsp page called when the method was closed for some reason, created by the author

_jspservice () {}: A method for handling JSP page created automatically by the JSP container, created by the JSP container, cannot be defined by the author.

When the JSP file is processed for the first time, he is converted into a servlet file. Then create a Servlet object, first execute the _jspinit () method to initialize the operation, because the entire execution process _jspinit () method executes only once, so you can do some of the necessary operations in this method such as connection to the database, initialization of some parameters, and then execute _ The Jspservice () method, which processes requests from the client, creates a thread for each request and creates multiple threads if there are multiple requests that need to be processed at the same time. Because the servlet is stored in memory for a long time, it executes fast, but because initialization requires compiling, the first execution is slower, and if for some reason the JSP Web page is closed or destroyed, the Jspdestroy () method is executed. Multi-threaded thinking of JSP

When multiple users request a JSP page, the Tomcat server initiates a thread for each client that is responsible for executing the bytecode file of the resident memory in response to the customer's request. These threads have a tomcat server to manage.

These threads share member variables (instance variables) of a JSP page, so any user action on a JSP page member variable affects other users, which can lead to thread insecurity. In order to ensure thread safety, we do not use (instance variable + class variable), it is so simple. You can also use the synchronized synchronization method, but this is inefficient.

The local variables in the method do not affect thread safety because they allocate space on the stack, and each thread has its own private stack space, and local variables in Java patches running on different threads do not interfere with each other.

JSP execution Process detailed

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.