The life cycle of the servlet

Source: Internet
Author: User


Although in the university's JSP specialized course "hears" the servlet, but at that time to the servlet's understanding, does not stop only to stay in "hears" the stage. Still remember just to the company internship at that time, reported a servlet's fault, the master asked me servlet configuration right? I came up with a sentence: "What configuration?" , the previous few days in the process of doing DRP, gradually deepened the understanding of the servlet. Let's talk about the understanding of the servlet.

(The article uses the business logic of landing to illustrate.) )

Initially, in Java Web Development, client and server worked by submitting requests to the servlet (such as submitting a user name, password), servlet processing (such as invoking business logic), and stitching the results into HTML format to return to the client:


Client:login.html         


Servlet:LoginServlet.java

Import java.io.ioexception;import javax.servlet.servletexception;import javax.servlet.http.*;/** * Login * @author Danny * /public class Loginservlet extends HttpServlet {@Overrideprotected void DoPost (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {super.doget (request, response); Response.setcontenttype ("Text/html;chartset=utf-8"); String username=request.getparameter ("UserName"); String password=request.getparameter ("password"); Boolean isloginsuccessful=manager.login (Username,password); isloginsuccessful) {response.getwriter (). Print ("Login successful! ");} Else{response.getwriter (). Print ("Login failed!" ");}}}

Manager Code omitted ~ ~

In addition to configuring the servlet in Web. xml:

  <servlet>  <servlet-name>LoginServlet</servlet-name> <!--servlet name, can be named  --- <servlet-class>com.danny.servlet.LoginServlet</servlet-class> <!--The true path of the servlet--  </ servlet>  <servlet-mapping>  <servlet-name>ClientIdValidateServlet</servlet-name> <!--servlet Name must match the name of the servlet above-  <url-pattern>/servlet/loginservlet</url-pattern><! --servlet virtual path, can be arbitrarily named, the action of the form's property value is it-  </servlet-mapping>
(the meaning of each label in the Web. XML configuration file can be seen in the following comments)



Each servlet is a Java class that inherits the Generateservlet class under the Javax.servlet package. Its life cycle is as follows:


After the ⑴ client sends the request, the servlet container (such as Tomcat) reads the configuration file, loads the servlet into memory, and instantiates it (one servlet is instantiated only once).

There are two ways in which Servlets are instantiated:

① is instantiated at Tomcat startup, and <load-on-startup>1</is added to the Profiles <servlet></servlet> tags Load-on-startup>. The servlet container is instantiated from small to large attribute values, that is, the smaller the <load-on-startup> attribute value is instantiated, the servlet with a value of 0 is finally instantiated. because servlet instantiation takes a long time, the benefit of this approach is that the user waits less time.

② is instantiated when the servlet is invoked, the<load-on-startup> property value is less than 0 o'clock, and Tomcat does not instantiate the servlet at startup< The Load-on-startup> property value is less than 0 by default.


When ⑵ is instantiated, the init () method is called (this method inherits from the Generateservlet Class) for initialization.


⑶ Create HttpResponse,HttpRequest objects, execute the service (HttpServletRequest req,httpservletresponse res) method, Depending on the type of client request (Post/get), the next step is to call the Dopost () method or call the Doget () method. (HttpServletRequest is responsible for receiving the data sent from the page, HttpServletResponse is responsible for writing data to the page)


When the ⑷ server stops, call the Destory () method to destroy the servlet instance.






The life cycle of the 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.