<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # `HttpServlet`類 > 原文: [https://beginnersbook.com/2013/05/http-servlet/](https://beginnersbook.com/2013/05/http-servlet/) 在 [Servlet API](https://beginnersbook.com/2013/05/servlet-api/) 中,我對`HttpServlet`進行了一些討論。在本文中,我將詳細討論`HttpServlet`。 與`GenericServlet`不同,`HTTPServlet`不會覆蓋`service()`方法。相反,它會覆蓋`doGet()`方法或`doPost()`方法或兩者。`doGet()`方法用于從服務器獲取信息,而`doPost()`方法用于向服務器發送信息。 在`HttpServlet`中,不需要覆蓋`service()`方法,因為此方法將 Http 請求分派給正確的方法處理程序,例如,如果它接收到 HTTP `GET`請求,則會將請求分派給`doGet()`方法。 ## `HttpServlet 如何工作? 正如您在下圖中看到的那樣,客戶端(用戶的瀏覽器)發出請求。這些請求可以是任何類型,例如 - `GET`請求,`POST`請求,`HEAD`請求等。服務器將這些請求分派給 servlet 的`service()`方法,此方法將這些請求分派給正確的處理程序,例如,如果它接收到`Get`請求它將其分派給`doGet()`方法。 ![Http Servlet](https://img.kancloud.cn/84/17/8417e60c714b7f7e05d8e8a053f45dd2_500x300.jpg) ## `HttpServlet`的層次結構 ```java java.lang.Object |_extended byjavax.servlet.GenericServlet |_extended byjavax.servlet.http.HttpServlet ``` 我已經在[`GenericServlet`文章](https://beginnersbook.com/2014/04/genericservlet-class/)中討論過你應該總是使用`HttpServlet`而不是`GenericServlet`。`HttpServlet`更易于使用,并且具有比`GenericServlet`更多的方法。 ## `HttpServlet`的例子 我在這個例子中使用 Eclipse IDE。從 Eclipse 文件菜單中創建新的`Dynamic Web Project`。 > 我已經解釋了在 Eclipse IDE 中創建 Servlet 的所有步驟,但是如果您不熟悉 Eclipse 并且沒有在系統上安裝它,請參考本指南:[如何安裝 Eclipse,配置 tomcat 并使用 Eclipse 運行第一個 Servlet 應用](https://beginnersbook.com/2017/07/how-to-create-and-run-servlet-in-eclipse-ide/)。 完成后,在 IDE 中創建以下所有文件后,項目結構(或層次結構)將如下所示。 ![Http Servlet Project Structure](https://img.kancloud.cn/d0/b7/d0b71f37fdc132ee0136c3ba0d4cee47_650x554.jpg) `index.html` 我們正在創建一個 html 文件,一旦我們點擊網頁上的鏈接就會調用 servlet。在`WebContent`文件夾中創建此文件。該文件的路徑應如下所示:`WebContent/index.html` ```java index<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Http Servlet Demo</title> </head> <body> <a href="welcome">Click to call Servlet</a> </body> </html> ``` `ExampleHttpServlet.java` 現在,我們通過擴展`HttpServlet`類來創建一個`HttpServlet`。右鍵單擊`src`文件夾并創建一個新的類文件,將該文件命名為`ExampleHttpServlet`。文件路徑應如下所示:`Java Resources/src/default package/ExampleHttpServlet.java` ```java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Creating Http Servlet by Extending HttpServlet class public class ExampleHttpServlet extends HttpServlet {?? ? private String mymsg; public void init() throws ServletException {? ? ? mymsg = "Http Servlet Demo";? ? } public void doGet(HttpServletRequest request, HttpServletResponse response)?throws ServletException, IOException {? ? ??? ? ? // Setting up the content type of web page? ? ? response.setContentType("text/html"); // Writing the message on the web page? ? ? PrintWriter out = response.getWriter();? ? ? out.println("<h1>" + mymsg + "</h1>");? ? ? out.println("<p>" + "Hello Friends!" + "</p>");? ? } public void destroy() {? ? ? // Leaving empty. Use this if you want to perform ? //something at the end of Servlet life cycle.? ? } } ``` `web.xml` 此文件可在此路徑`WebContent/WEB-INF/web.xml`中找到。在此文件中,我們將使用特定 URL 映射 Servlet。由于我們在單擊`index.html`頁面上的鏈接時調用歡迎頁面,因此我們將歡迎頁面映射到我們上面創建的 Servlet 類。 ```java <web-app> <display-name>BeginnersBookServlet</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>MyHttpServlet</servlet-name> <servlet-class>ExampleHttpServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyHttpServlet</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app> ``` **運行項目:** 右鍵單擊`index.html`,在服務器上運行。 **輸出:** ![Http Servlet Output1](https://img.kancloud.cn/22/39/223978624a89b3a3de84fa67d7de711d_1024x159.jpg) 單擊鏈接后,您將看到此屏幕: ![Http Servlet Output2](https://img.kancloud.cn/dc/30/dc3053987ae33d0f1d6dcbc3ac34d1de_1024x226.jpg) ## `HttpServlet`類的方法 1. `protected void doGet(HttpServletRequest req, HttpServletResponse resp)`:這個方法由 servlet `service`方法調用,以處理來自客戶端的 HTTP `GET`請求。覆蓋此方法時,請讀取請求數據,編寫響應頭,獲取響應的編寫器或輸出流對象,最后編寫響應數據。 2. `protected long getLastModified(HttpServletRequest req)`:返回一個長整數,指定上次修改`HttpServletRequest`對象的時間,格林威治標準時間 1970 年 1 月 1 日午夜(以秒為單位),如果時間不知道,則返回 -1 3. `protected void doHead(HttpServletRequest req, HttpServletResponse resp)`:這個方法由 servlet `service`方法調用,以處理來自客戶端的 HTTP `HEAD`請求。當客戶端想要僅查看響應的標頭(例如`Content-Type`或`Content-Length`)時,它會發送`HEAD`請求 4. `protected void doPost(HttpServletRequest req, HttpServletResponse resp)`:servlet `service`方法調用此方法來處理來自客戶端的`POST`請求。 HTTP `POST`方法允許客戶端一次性向 Web 服務器發送無限長度的數據,并且在向服務器發布信息時非常有用。與`doGet`不同,我們從服務器獲取信息時,在從客戶端向服務器傳輸信息時使用此方法。 5. `protected void doPut(HttpServletRequest req, HttpServletResponse resp)`:這個方法由 servlet `service`方法調用,以處理來自客戶端的`PUT`請求。此方法類似于`doPost`方法,但與我們向服務器發送信息的`doPost`方法不同,此方法將文件發送到服務器,這類似于從客戶端到服務器的 FTP 操作。 6. `protected void doDelete(HttpServletRequest req, HttpServletResponse resp)`:由 servlet `service()`方法調用,以處理來自客戶端的`DELETE`請求,允許客戶端從服務器刪除文檔,網頁或信息。 7. `protected void doOptions(HttpServletRequest req, HttpServletResponse resp)`:由`service`方法調用,以允許 servlet 處理`OPTIONS`請求。`OPTIONS`請求確定服務器支持哪些 HTTP 方法并返回適當的標頭。 8. `protected void doTrace(HttpServletRequest req, HttpServletResponse resp)`:此方法由`service()`方法調用,用于處理`TRACE`請求。用于調試目的。 9. `protected void service(HttpServletRequest req, HttpServletResponse resp)`:沒有必要覆蓋此方法,此方法從客戶端接收 HTTP 請求并將它們轉發到相應的 doXXX 方法,如`doGet()`,`doPost()`,`doHEAD()`等 10. `public void service(ServletRequest req, ServletResponse res)`:將客戶端請求轉發給受保護的`service`方法。也沒有必要重寫此方法。 **參考**:[`HttpServlet`官方文檔](https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServlet.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看