HttpServlet Service method

Source: Internet
Author: User

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

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.