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.