HttpServlet Service方法

來源:互聯網
上載者:User

標籤:響應   ted   eset   doget   get   orm   mat   str   row   

service() 方法是執行實際任務的主要方法。Servlet 容器(即 Web 服務器)調用 service() 方法來處理來自用戶端(瀏覽器)的請求,並把格式化的響應寫回給用戶端。

每次伺服器接收到一個 Servlet 請求時,伺服器會產生一個新的線程並調用服務。service() 方法檢查 HTTP 要求類型(GET、POST、PUT、DELETE 等),並在適當的時候調用 doGet、doPost、doPut,doDelete 等方法。

下面是該方法的特徵:

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{}

service() 方法由容器調用,service 方法在適當的時候調用 doGet、doPost、doPut、doDelete 等方法。所以,您不用對 service() 方法做任何動作,您只需要根據來自用戶端的請求類型來重寫 doGet() 或 doPost() 即可。

 

/*     */   protected void service(HttpServletRequest req, HttpServletResponse resp)/*     */     throws ServletException, IOException/*     */   {/* 568 */     String method = req.getMethod();/*     */     /* 570 */     if (method.equals("GET")) {/* 571 */       long lastModified = getLastModified(req);/* 572 */       if (lastModified == -1L)/*     */       {/*     */ /* 575 */         doGet(req, resp);/*     */       } else {/* 577 */         long ifModifiedSince = 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 */       long lastModified = 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);/*     */     }/* 600 */     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 = new Object[1];/* 617 */       errArgs[0] = method;/* 618 */       errMsg = MessageFormat.format(errMsg, errArgs);/*     */       /* 620 */       resp.sendError(501, errMsg);/*     */     }/*     */   }

 

HttpServlet Service方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.