The service () method is the primary way to perform the actual task. The Servlet container (that is, the WEB server) invokes the service () method to process requests from the client (browser) and writes the formatted response back to the client.
Each time the server receives a Servlet request, the server generates a new thread and invokes the service. The service () method checks the HTTP request type (GET, POST, PUT, DELETE, and so on) and calls Doget, DoPost, Doput,dodelete, and so on, when appropriate.
Here are the characteristics of the method:
Publicvoid service(servletrequest request,servletresponse response) throwsservletexception,ioexception{}
The service () method is called by the container, and the service method calls Doget, DoPost, DoPut, DoDelete, and so on when appropriate. Therefore, you do not need to do any action on the service () method, you only need to override Doget () or DoPost () based on the request type from the client.
/* */ protected voidService (HttpServletRequest req, HttpServletResponse resp)/* */ throwsservletexception, IOException/* */ {/*568*/String method =Req.getmethod ();/* */ /*570*/ if(Method.equals ("GET")) {/*571*/ LongLastModified =getlastmodified (req);/*572*/ if(LastModified = = -1l)/* */ {/* */ /*575*/doget (req, resp);/* */}Else {/*577*/ LongIfmodifiedsince = Req.getdateheader ("If-modified-since");/*578*/ if(Ifmodifiedsince < lastmodified/1000l * 1000L)/* */ {/* */ /* */ /*582*/maybesetlastmodified (resp, lastmodified);/*583*/doget (req, resp);/* */}Else {/*585*/Resp.setstatus (304);/* */ }/* */ }/* */ }/*589*/ Else if(Method.equals ("HEAD")) {/*590*/ LongLastModified =getlastmodified (req);/*591*/maybesetlastmodified (resp, lastmodified);/*592*/Dohead (req, resp);/* */ }/*594*/ Else if(Method.equals ("POST")) {/*595*/doPost (req, resp);/* */ }/*597*/ Else if(Method.equals ("PUT")) {/*598*/DoPut (req, resp);/* */ }/* -*/ Else if(Method.equals ("DELETE")) {/*601*/DoDelete (req, resp);/* */ }/*603*/ Else if(Method.equals ("Options")) {/*604*/dooptions (req, resp);/* */ }/*606*/ Else if(Method.equals ("TRACE")) {/*607*/Dotrace (req, resp);/* */ /* */ /* */ }/* */ Else/* */ {/* */ /* */ /*615*/String errmsg = lstrings.getstring ("http.method_not_implemented");/*616*/object[] Errargs =NewObject[1];/*617*/Errargs[0] =method;/*618*/ErrMsg =Messageformat.format (errmsg, Errargs);/* */ /*620*/Resp.senderror (501, errmsg);/* */ }/* */}
HttpServlet Service method