Definition
public class HttpServlet extends Genericservlet implements Serializable
This is an abstract class used to simplify the process of writing HTTP Servlet. It is an extension of the Genericservlet class and provides a framework for handling HTTP protocols. The service method in this class supports standard HTTP methods such as GET, POST, and so on. This support process is achieved by assigning them to appropriate methods (such as Doget, DoPost).
method
1, DoDelete
protected void DoDelete (HttpServletRequest request,httpservletresponse response)
throws Servletexception, IOException;
Called by the service method of this class to handle an HTTP DELETE operation. This operation allows the client to request that the URL be removed from the server. This action may have a negative impact on the user and take responsibility for it. The default execution result of this method is to return an HTTP bad_request error. When you want to handle the DELETE
When requested, you must overload this method.
2, Doget
protected void doget (HttpServletRequest request,httpservletresponse response)
Throws Servletexception,ioexception;
Called by the service method of this class to handle an HTTP GET operation. This operation allows the client to simply "get" resources from an HTTP server. Overloading of this method will automatically support the HEAD method. The GET operation should be secure and not adversely affected. This operation should also be safe to repeat. The default execution result of this method is to return an HTTP bad_request error.
3, Dohead
protected void Dohead (HttpServletRequest request,httpservletresponse response)
Throws Servletexception,ioexception;
Called by the service method of this class to handle an HTTP HEAD operation. By default, this operation is performed according to an unconditional GET method, which does not return any data to the client, but simply returns the header information that contains the length of the content. As with the GET operation, this operation should be secure and not adversely affected. This operation should also be safe to repeat. The default execution of this method is to automatically handle the HTTP HEAD operation, which does not need to be performed by a subclass.
4, Dooptions
protected void Dooptions (HttpServletRequest request,httpservletresponse response)
Throws Servletexception,ioexception;
Called by the service method of this class to handle an HTTP OPTION operation. This operation automatically determines which HTTP method to support. For example, a Servlet writes a httpservlet subclass and overloads the Doget method, Dooption returns the following header: Allow:get,head,trace,options
You generally do not need to overload this method.
5, DoPost
protected void DoPost (HttpServletRequest request,httpservletresponse response)
Throws Servletexception,
IOException;
Called by the service method of this class to handle an HTTP POST operation. This operation contains the data for the request body, and the Servlet should follow him. This operation may have a negative effect. For example, update stored data or online shopping. The default execution result of this method is to return an HTTP bad_request error. When you want to handle the post operation, you must overload this method in HttpServlet subclasses.
6, DoPut
protected void DoPut (HttpServletRequest request,httpservletresponse response)
throws Servletexception, IOException;
Called by the service method of this class to handle an HTTP PUT operation. This operation is similar to sending files via FTP. This operation may have a negative effect. For example, update stored data or online shopping. The default execution result of this method is to return an HTTP bad_request error. When you're going to handle the PUT exercise
, you must overload this method in HttpServlet subclasses.
7, Dotrace
protected void Dotrace (HttpServletRequest request,httpservletresponse response)
throws Servletexception, IOException;
Called by the service method of this class to handle an HTTP TRACE operation. The default execution result of this operation is to produce a response that contains a message that reflects all the header fields sent in the trace request. When you develop a Servlet, in most cases you will need to reload this method.
8, Getlastmodified
Protected long getlastmodified (HttpServletRequest request);
Returns the last modified time for this request entity. In order to support a GET operation, you must overload this method to accurately reflect the time of the last modification. This will help browsers and proxy servers reduce load servers and network resources to work more efficiently. The value returned is the number of milliseconds since the 1970-1-1 Day (GMT). The default execution result is to return a negative number, which indicates that the last modification time is unknown and it cannot be used by a conditional get operation.
9. Service
protected void Service (HttpServletRequest request,httpservletresponse response)
throws Servletexception, IOException;
Public void Service (ServletRequest request, servletresponse response)
Throws Servletexception, IOException;
This is a Servlet http-specific scheme that allocates other methods that request support for this request to this class. When you develop a Servlet, in most cases you do not have to overload this method. The 2 methods that need to be overloaded in actual development are doget and dopost. Then generally rewrite the Dopost method, and then rewrite doget only need to adjust the Dopost method. The following post-HttpServlet class source code.
Package Javax.servlet.http;import Java.io.ioexception;import Java.io.serializable;import Java.lang.reflect.Method; Import Java.text.messageformat;import Java.util.enumeration;import Java.util.resourcebundle;import Javax.servlet.genericservlet;import Javax.servlet.servletexception;import Javax.servlet.ServletOutputStream; Import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;public abstract class HttpServlet extends Genericservlet implements serializable{private static final String Method_delete = "DELETE"; private static final String Method_head = "HEAD"; private static final String Method_get = "GET"; private static final String method_options = "OPTIONS"; private static final String Method_post = "POST"; private static final String Method_put = "PUT"; private static final String Method_trace = "TRACE"; private static final String header_ifmodsince = "If-modified-since"; private static final String Header_lastmod = "last-modified"; private static Final String lstring_file = "javax.servlet.http.LocalStrings"; private static ResourceBundle lstrings = Resourcebundle.getbundle ("javax.servlet.http.LocalStrings"); protected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {Strin G protocol = Req.getprotocol (); String msg = lstrings.getstring ("http.method_get_not_supported"); if (Protocol.endswith ("1.1")) Resp.senderror (405, MSG); else Resp.senderror (n, msg); } Protected Long getlastmodified (HttpServletRequest req) {return-1l; } protected void Dohead (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {N Obodyresponse response = new Nobodyresponse (RESP); Doget (req, response); Response.setcontentlength (); } protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {S Tring protocol = Req.getprotocol (); String msg = lstrings.getstring ("http.method_pOst_not_supported "); if (Protocol.endswith ("1.1")) Resp.senderror (405, MSG); else Resp.senderror (n, msg); } protected void DoPut (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {St Ring protocol = Req.getprotocol (); String msg = lstrings.getstring ("http.method_put_not_supported"); if (Protocol.endswith ("1.1")) Resp.senderror (405, MSG); else Resp.senderror (n, msg); } protected void DoDelete (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { String protocol = Req.getprotocol (); String msg = lstrings.getstring ("http.method_delete_not_supported"); if (Protocol.endswith ("1.1")) Resp.senderror (405, MSG); else Resp.senderror (n, msg); } private static method[] Getalldeclaredmethods (Class c) {if (C.equals (Httpservlet.class)) {return null; } method[] Parentmethods = Getalldeclaredmethods (C.getsuperclass ()); Method[] ThismEthods = C.getdeclaredmethods (); if ((parentmethods! = null) && (parentmethods.length > 0)) {method[] allmethods = new Method[parentmethod S.length + thismethods.length]; System.arraycopy (parentmethods, 0, allmethods, 0, parentmethods.length); System.arraycopy (thismethods, 0, Allmethods, Parentmethods.length, thismethods.length); Thismethods = Allmethods; } return thismethods; } protected void Dooptions (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { Method[] methods = Getalldeclaredmethods (GetClass ()); Boolean allow_get = false; Boolean allow_head = false; Boolean allow_post = false; Boolean allow_put = false; Boolean allow_delete = false; Boolean allow_trace = true; Boolean allow_options = true; for (int i = 0; i < methods.length; i++) {Method m = methods[i]; if (M.getname (). Equals ("Doget")) {Allow_get = true; Allow_head = true; } if (M.getname (). Equals ("DoPost")) Allow_post = true; if (M.getname (). Equals ("DoPut")) Allow_put = true; if (M.getname (). Equals ("DoDelete")) {Allow_delete = true; }}, String allow = null; if ((Allow_get) && (allow = = null)), allow = "GET"; if (Allow_head) if (allow = = null) allow = "HEAD"; else Allow = Allow + ", HEAD"; if (allow_post) if (allow = = null) allow = "POST"; else Allow = Allow + ", POST"; if (allow_put) if (allow = = nulls) allow = "PUT"; else Allow = Allow + ", PUT"; if (Allow_delete) if (allow = = null) allow = "DELETE"; else Allow = Allow + ", DELETE"; if (allow_trace) if (allow = = null) allow = "TRACE"; else Allow = Allow + ", TRACE"; if (allow_options) {if (allow = = nulls) allow = ' OPTIONS '; else allow = allow + ', OPTIONS '; } resp.setheader ("Allow", allow); } protected void Dotrace (HttpServletRequest req, httpservletresPonse resp) throws Servletexception, IOException {String CRLF = "\ r \ n"; String responsestring = "TRACE" + req.getrequesturi () + "" + Req.getprotocol (); Enumeration reqheaderenum = Req.getheadernames (); while (Reqheaderenum.hasmoreelements ()) {String headername = (string) reqheaderenum.nextelement (); responsestring = responsestring + CRLF + headername + ":" + req.getheader (headername); } responsestring = responsestring + CRLF; int responselength = Responsestring.length (); Resp.setcontenttype ("Message/http"); Resp.setcontentlength (ResponseLength); Servletoutputstream out = Resp.getoutputstream (); Out.print (responsestring); Out.close (); } protected void Service (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { String method = Req.getmethod (); if (Method.equals ("GET")) {Long lastmodified = getlastmodified (req); if (lastmodified = = -1l) {doget (req, resp); } ELSE {LONG ifmodifiedsince = Req.getdateheader ("if-modified-since"); if (Ifmodifiedsince < lastmodified/1000l * 1000L) {maybesetlastmodified (resp, lastmodified); Doget (req, resp); } else {resp.setstatus (304); }}}} else if (Method.equals ("HEAD")) {Long lastmodified = getlastmodified (req); Maybesetlastmodified (resp, lastmodified); Dohead (req, resp); } else if (Method.equals ("POST")) {DoPost (req, resp); } else if (Method.equals ("PUT")) {DoPut (req, resp); } else if (Method.equals ("DELETE")) {DoDelete (req, resp); } else if (Method.equals ("Options")) {dooptions (req, resp); } else if (Method.equals ("TRACE")) {Dotrace (req, resp); } else {String errmsg = lstrings.getstring ("http.method_not_implemented"); object[] Errargs = new Object[1]; Errargs[0] = method; ErrMsg = Messageformat.format (errmsg, Errargs);Resp.senderror (501, errmsg); }} private void Maybesetlastmodified (HttpServletResponse resp, long lastmodified) {if (Resp.containsheader ("Last-m Odified ")) return; if (lastmodified >= 0L) resp.setdateheader ("Last-modified", lastmodified); public void Service (ServletRequest req, servletresponse Res) throws Servletexception, IOException {HTTPSERVLETR Equest request; HttpServletResponse response; try {request = (httpservletrequest) req; Response = (httpservletresponse) res; } catch (ClassCastException e) {throw new Servletexception ("Non-http Request or response"); } service (request, response); }}
Servlet--httpservlet class