Jsp/servlet Working principle

Source: Internet
Author: User

Jsp/servlet working principle Servletservlet No Main method, can not run independently, its operation needs the support of the container, Tomcat is the most common Jsp/servlet container.
The servlet runs in the servlet container, and the container manages the entire process from creation to destruction.
The life cycle of the Servlet (1) Loading and instantiating
The servlet container loads and instantiates a servlet. Creates an instance of the Servlet class.
(2) initialization
After the servlet instantiation is complete, the container is responsible for invoking the Init () method of the servlet instance to do some additional initialization before processing the user request.
(3) Processing requests
When the servlet container receives a servlet request, it runs the service () method of the corresponding servlet instance, and the service () method dispatches the run to the request.
Doxx (Doget,dopost) method to handle user requests.
(4) Destruction
When the servlet container decides to remove a servlet from the server (such as a servlet file being updated), it invokes the Destroy () method of the Servlet instance, before destroying the servlet instance.
To do some other work.
where (1) (2) (4) is executed only once throughout the Servlet's life cycle.
The Servlet works in conjunction with the flowchart given on the right:
When the client browser requests a Servlet from the server, the server receives the request and first retrieves the request from the container.
Whether a matching Servlet instance already exists. If it does not exist, the servlet container is responsible for loading and instantiating the class servlet
An instance object, and the container framework is responsible for invoking the instance's Init () method to do some initialization work on the instance, and then
The Servlet container runs the service () method for the instance.
If the Servlet instance already existsin, the container framework calls the service () method of the instance directly. At run time, the service () method automatically dispatches the Doxx () method that corresponds to the user request to respond to a user-initiated request.
Typically, there is only one instance of each Servlet class in the container, and whenever a request arrives, a thread is assigned to process the request.







JSP working principle combined with the flow chart on the right:When the client browser requests a JSP page from the server, the server receives the request and first checks the requested
Whether the JSP file content (code) has been updated, or whether the JSP file was first accessed after it was created, and if so,
Then, this JSP file will be translated into a Servlet class Java source code on the server side of the JSP engine
File. The Servlet class is then compiled into a byte-code file with the Java compiler, and loads
To the JVM to interpret the execution. The rest is equivalent to the Servlet process.
If the requested JSP file content (code) has not been modified, then its processing is also equivalent to a Servlet's
The processing process. The corresponding Servlet instance is retrieved directly from the server for processing.

It is important to note that the JSP file is not converted to a Servlet class when the server is started. Instead, it is accessed by the client
When the conversion is possible (such as the JSP file content is not updated, etc., it does not occurServlet conversion). In the case of Tomcat, open the directory%tomcat%/work/% your project directory%, and you will see that there are 3
Subdirectories: org/apache/jsp, without these 3 directories, the project's JSP file has not been accessed,
Open into the JSP directory, you will see some *_jsp.java and *_jsp.class files, this is the JSP file is converted to
The source and bytecode files of the Servlet class.
If you are interested, you canusing a browser to visitAsk the server, and then observe the JSP conversion Servlet and the timing of the compilation. The servlet and jspjsp essence is a servlet, and its operation also requires container support.
Java and HTML code can be written in both JSP and Servlet files, unlike
Although the Servlet can also dynamically generate page content, it is more inclined to control logic.
The JSP is eventually converted into a Servlet to interpret execution in the JVM, although it can also write Java code in the JSP, but it is more biased towards the presentation of the page view.
In the MVC architecture pattern, in the case of JSPs and Servlets, C is typically used by servlets, and V is typically used by JSPs as a function.

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.