There are two basic packages for all servlets that need to be written in a servlet: Javax.servlet and Javax.servlet.http.
The following mainly describes the servlet provided by Javax.servlet and the HttpServlet application programming interface provided by Javax.servlet.http.
Servlet Source code:
Package Javax.servlet;public interface servlet{public abstract void init (ServletConfig servletconfig) throws Se Rvletexception; Public abstract ServletConfig getservletconfig (); public abstract void Service (ServletRequest servletrequest, Servletresponse servletresponse) throws Servlet Exception, IOException; Public abstract String getservletinfo (); public abstract void Destroy ();}
The above is the servlet source code, the servlet itself is an interface, but provides a few abstract methods.
Init () method
In the servlet's life cycle, only the init () method is executed once, which is executed when the server loads the servlet. By configuring the server, you can set the first access servlet to the servlets in the boot server or client. Init () is not repeated regardless of how many clients access the servlet.
Service () method
The service () method is the body part of the servlet. Each request to a HttpServlet object by the customer invokes the service () method of the object and is passed to the method a "request" object and a "response" object as a parameter. The Request object provides information about the request, and the response object provides a means of communication that returns the response information to the browser. The related classes in the Javax.servlet package are ServletRequest and Servletresponse, and the related classes in the Javax.servlet.http package are httpservletrequest and HttpServletResponse. The Servlet communicates with the server through these objects and eventually communicates with the client. A servlet can learn about the client environment, the server environment, and all the information provided by the client by invoking the method of the Request object, and by invoking the method of the Response object, the servlet sends a response to the client.
The service () method in HttpServlet The default services feature is to invoke the DO function corresponding to the method of the HTTP request. For example, if the HTTP request method is GET, Doget () is called by default. The DoPost () method is called when a client issues an HTTP POST request through an HTML form. The parameters associated with the POST request are sent from the browser to the server as a separate HTTP request. You should use the Dopost () method when you need to modify the server-side data.
The response of a servlet can be of the following types:
An output stream, which is interpreted by the browser based on its content type (such as text/html).
An HTTP error response, redirected to another URL, servlet, and JSP.
Destroy () method
The Destroy () method executes only once, which is the method that is executed when the server is stopped and the servlet is unloaded. When the server unloads the Servlet, the Destroy () method is called after all service () method calls are complete, or after the specified interval. A servlet may produce additional threads when it runs the service () method, so when calling the Destroy () method, you must confirm that the threads have been terminated or completed.
Getservletconfig () method
The Getservletconfig () method returns an ServletConfig object that is used to return initialization parameters and ServletContext. The ServletContext interface provides environment information about the servlet.
Getservletinfo () method
The Getservletinfo () method is an optional method that provides information about the servlet, such as author, version, and copyright.
Source code for HttpServlet:
Package Javax.servlet.http;public abstract class HttpServlet extends Genericservlet implements serializable{... this Omit n characters at a
From the HttpServlet source definition can be seen, HttpServlet and Servelt No association, but inherited Genericservlet, in fact, Genericservlet is an abstract class, the class has implemented the servlet, ServletConfig, serializable these three interfaces.
As can be seen in the source code of HttpServlet, this class has two service () methods, the following we describe each:
Public void service (servletrequest req, servletresponse res) throws ServletException, IOException { HttpServletRequest request; httpservletresponse response; try { request = (HttpServletRequest) req; response = (HttpServletResponse) res; } catch (classcastexception e) { throw new servletexception (" Non-http request or response "); } &Nbsp; service (request, response);}
Through code discovery, this is simply converting the corresponding request and response to the corresponding object based on the HTTP protocol. A service method is defined in the
Servlet interface, and the method is implemented by HttpServlet. Convert ServletRequest and Servletresponse to HttpServletRequest and HttpServletResponse.
Protected void service (HTTPSERVLETREQUEST&NBSP;REQ,&NBSP;HTTPSERVLETRESPONSE&NBSP;RESP) throws ServletException, IOException { string method = req.getmethod (); if (Method.equals (method_get)) { long lastmodified = getlastmodified (req); if (lastmodified == -1) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;SERVLET&NBSP;DOESN ' t support if-modified-since, no reason // to go through further expensive logic &Nbsp; doget (REQ,&NBSP;RESP); } else { long ifModifiedSince = Req.getdateheader (header_ifmodsince); if (ifmodifiedsince < (lastmodified / 1000 * )) { // if the servlet mod time is later, call doget () // round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less Maybesetlastmodified (resp, lastmodified); doget (REQ,&NBSP;RESP); } else { Resp.setstatus (httpservletresponse.sc_not_modified); } } &nBSP;} else if (Method.equals (method_head)) { long lastmodified = getlastmodified (req); maybesetlastmodified (resp, lastmodified); dohead (REQ,&NBSP;RESP); } else if (Method.equals (method_post)) { dopost (REQ,&NBSP;RESP); } else if (Method.equals (method_put)) { doput (REQ,&NBSP;RESP); } else if (Method.equals (method_delete)) { &nbSp; dodelete (REQ,&NBSP;RESP); } else if (Method.equals (method_options)) { dooptions (REQ,RESP); } else if (Method.equals (method_trace)) { dotrace (REQ,RESP); } else { // // Note that this means NO servlet Supports whatever // method was requested, anywhere on this server. // String errMsg = Lstrings.getstring ("http.method_not_implemented"); Object[] errArgs = new Object[1]; errArgs[0] = method; errmsg = messageformat.format (Errmsg, errargs); resp.senderror (httpservletresponse.sc_not_implemented, ERRMSG); }}
Through the service () method, the client's request is first submitted to the service method, the method name of the submission is obtained, and the appropriate method is called according to the method name. If you override this method, you will not invoke other specific methods based on the method name .
Take a look at several doxxx () methods inside the HttpServlet class:
protected void doxxx (httpservletrequest req, HTTPSERVLETRESPONSE&NBSP;RESP) throws servletexception, ioexception { string protocol = req.getprotocol (); string msg = Lstrings.getstring ("http.method_xxx_not_supported"); if ( Protocol.endswith ("1.1")) { Resp.senderror (405, msg); } else { resp.senderror (400, msg); }}
The Discovery Doxxx () method simply determines the protocol type, then throws the corresponding exception, and nothing else is done. So, when implementing your servlet, you need to rewrite these methods to suit your needs. You generally do not need to override the service method.
Source parsing servlet and HttpServlet