Servlet lifecycle and Response process detailed

Source: Internet
Author: User
1.Servlet life cycle

As shown in the following illustration:

As you can get from the diagram above, the servlet is a single-instance, thread-safe. 2.Servlet Response Process

The servlet engine checks whether an instance object for the servlet has been loaded and created. If so, perform step fourth directly, otherwise, perform the second step.

Loads and creates an instance object for the servlet, calling the Servlet's construction method.

Invokes the Init () method of the Servlet instance object.

Creates a ServletRequest object to encapsulate the request and a Servletresponse object that represents the response message. The servlet's service () method is then invoked, and the request and response objects are passed in as parameters.

Before the Web application is stopped or restarted, the servlet engine unloads the servlet and invokes the servlet's Destroy method before the servlet unloads. 3.Servletconfig

Encapsulates the configuration information for the servlet and can get the ServletContext object. Automatically load 4.ServletContext from Tomcat

The servlet engine creates a corresponding ServletContext object for each Web application, which is contained in the ServletConfig object.

Because all the servlet in a Web application shares the same ServletContext object, the ServletContext object is called the Application object (the Web Application object). 5.Service ()

Service () –servlet execution body, called by the server; 6.ServletRequest

servletrequest– encapsulates the request information, which is created by the server;

General use when strong to HttpServletRequest

Method Example:

1.String user = Request.getparameter ("user");

2.String [] interestings = Request.getparametervalues ("interesting");

3.enumeration<string> names = Request.getparameternames ();
        while (Names.hasmoreelements ()) {
            String name = Names.nextelement ();
            String val = request.getparameter (name);

            System.out.println ("^^" + name + ":" + val);
        }

4.map<string, string[]> Map = Request.getparametermap ();
        For (map.entry<string, string[]> entry:map.entrySet ()) {
            System.out.println ("* *" + entry.getkey () + ":" + Arrays.aslist (Entry.getvalue ());  
        }

5.String RequestUri = Httpservletrequest.getrequesturi ();

6.String method = Httpservletrequest.getmethod ();

7.String querystring = Httpservletrequest.getquerystring ();

8.String Servletpath = Httpservletrequest.getservletpath ()
7.ServletResponse

The

Encapsulates response information, is created by the server, and generally becomes httpservletresponse when used.

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.