Request the servlet process and the servlet life cycle

Source: Internet
Author: User

I. Servlet request flow (servlet working principle parsing)

Two. The life cycle of the servlet

1. The client sends a request to the server.

2. The server begins to accept that the servlet instance of the request is present, and if it does not exist, mount a servlet class and create the instance first.

If present, call the Servlet's service method directly, and then decide whether to call the Doget method or the Dopost method.

3.servlet After creating the instance, call the Init method to initialize. The Servce method is then called to determine whether to call the Doget method or the Dopost method.

4. Finally determine if the server is down and call the Destroy method if it is turned off.

Three. Tomcat's servlet container loading servlet:

1, automatic loading: Some servlets need to be loaded in the servlet container when it is started, you need to add the priority code in its <Servlet> tag under Web. xml:

<Servlet>

<location-startup>1<location-startup>

</Servlet>

The smaller the number, the higher the priority of the servlet, which is preceded by the lower priority of the other auto-mounts.

2. When the servlet container is started, the client sends a request to a servlet for the first time, and the Tomcat servlet container loads a servlet.

3. When the servlet class file is updated, it will reload.

4. Servlets reside in memory for a long time. Once a servlet is loaded, it will remain in the server's memory for a long time until the server shuts down.

5. After the servlet is loaded, the servlet container creates a servlet instance and invokes the servlet's init () method for initialization. The init () method is called only once throughout the servlet's life cycle.

Servlet3.0 new features detailed

Four: servlet/jsp nine large built-in objects

V: The path of the servlet jumps

First, the JSP jumps to the servlet

1, relative path, such as href= "Servlet/xxservlet", note: if written as "/servletxxservlet" will be an error, because the first '/' means "Server root directory http://localhost:8080/"

2, absolute path, through the built-in member variable path implementation, such as href= "<%=path%>/servlet/xxservlet"

Note: The path here is the project root directory, such as "Http://localhost:8080/xxProjectDemo"

Second, servlet jump JSP

1, Request redirect: Response.sendredirect (Request.getcontextpath () + "/xxx.jsp");

The project root is obtained through the Request.getcontextpath () method, or through the ".. /xxx.jsp "Get on the path obtained

2, the server internal forwarding: Request.getrequestdispatcher (". /xxx.jsp "). Forward (REQ,RESP);

Note: The Url-pattern in Web. XML must start with/, here/represent the root directory of the project.

VI: Get initialization parameters in servlet

The initialization parameters are set first and can have several parameters.

<servlet>
<servlet-name>xxServlet</servlet-name>
<servlet-class>com.xxx.xxServlet</servlet-class>
<init-param>
<param-name>xxname</param-name>
<param-value>xxvalue</param-value>
</init-param>
</servlet>

Get value in servlet: String xxvalue = This.getinitparameter ("Xxname");

Request the servlet process and the servlet life cycle

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.