Servlet life cycle

Source: Internet
Author: User

Container InitializationWhen the servlet container (Tomcat) is started, it deploys and loads all of the web apps. When the web app is loaded, the servlet container creates a servlet context (ServletContext) for each app at once and stores it in memory. The servlet container processes the Web. xml file, and once created a set of methods defined in the servlet, filter, and listener ServletContext in XML. The servlet uses these methods to communicate with its servlet container.  For example, to get the MIME type of a file, to forward a request, or to write a log file. When a client (user using a Web browser) sends an HTTP request, the servlet container creates a new HttpServletRequest and HttpServletResponse object. and pass them to the method that already created the filter and URL pattern to match the request URL of the servlet instance, all of which use the same thread. and ServletContext through a mapping class. Map to the appropriate servlet container servlet ProcessServlet class diagram: In fact, the servlet is an interface defined in the Javax.servlet package. It declares three basic methods--init (), service (), and Destroy () for the life cycle of a servlet. Each servlet implements these methods (defined in the SDK or user-defined) and is called by the server at a specific time in their life cycle. 1.Servlet by calling Init ()method is initialized. There are three classes that are actively associated with servlets, namely ServletConfig, ServletRequest, and Servletresponse. The servlet uses ServletContext to pass parameters and build an interactive scene. ServletConfig is passed from the container when the Servlet init, servletconfig the configuration information to ServletContext. ServletRequest and Servletresponse encapsulate the interactive information. They eventually turned into HttpServletRequest and HttpServletResponse. 2. The servlet's Init method is called only once throughout the servlet's life cycle. Each access request to a servlet causes the servlet engine to invoke the Servlet's service method once. For each access request, the Servlet engine creates a new HttpServletRequest request object and a new HttpServletResponse response object. The two objects are then passed as arguments to the service () method of the servlet that it invokes, and the service method calls the Doxxx method separately, depending on the request. 3. Destory is responsible for destroying the Servlet, which is garbage collected by the JVM's garbage collector. SessionWhen a client accesses the Web app for the first time or gets httpsession for the first time using Request.getsession (), the servlet container creates a session that generates a long type unique ID (you can use the Session.getid () to get it) and save it in the server's memory. The servlet container also sets a cookie,cookie name of Jsessionid in the HTTP response and the value of the cookie is the unique ID of the session. Thread SafetyWhen a client requests a servlet for the first time, the servlet container instantiates the Servlet class based on the Web. XML configuration file. When a new client requests the servlet, the servlet class is typically no longer instantiated, that is, multiple threads are using the instance. Resolution: 1. Implementation SinglethreadmodelInterface this interface is equivalent to each request when the read instantiation of a servlet, is not recommended 2. Potentially shared data is manually locked using synchronized 3. Avoid instance variables reference: https://www.ibm.com/developerworks/cn/ Java/j-lo-servlet/http://www.cnblogs.com/cuiliang/archive/2011/10/21/2220671.html http://www.jiagoushuo.com/ Article/1000047.html

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.