<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Servlet ## HTTP簡介 WEB瀏覽器與WEB服務器之間的一問一答的交互過程必須遵循一定的規則,這個規則就是HTTP協議。 HTTP是 hypertext transfer protocol(超文本傳輸協議)的簡寫,它是 TCP/IP 協議集中的一個應用層協議,用于定義WEB瀏覽器與WEB服務器之間交換數據的過程以及數據本身的格式。 ## HTTP 的會話方式 四個步驟: ![](https://box.kancloud.cn/4a448c710d4e266debc91469fff706a4_396x128.png) 瀏覽器與WEB服務器的連接過程是短暫的,每次連接只處理一個請求和響應。對每一個頁面的訪問,瀏覽器與WEB服務器都要建立一次單獨的連接。 瀏覽器到WEB服務器之間的所有通訊都是完全獨立分開的請求和響應對。 ![](https://box.kancloud.cn/143ffdd88ae8916c3c650342445b24b1_523x395.png) ## HTTP請求消息(了解即可) 請求消息的結構: 一個請求行、若干消息頭、以及實體內容,其中的一些消息頭和實體內容都是可選的,消息頭和實體內容之間要用空行隔開。 ![](https://box.kancloud.cn/6b6433c3afd55e2dcabef20e41fb4f3c_771x314.png) ## HTTP響應消息(了解即可) 響應消息的結構: 一個狀態行、若干消息頭、以及實體內容 ,其中的一些消息頭和實體內容都是可選的,消息頭和實體內容之間要用空行隔開。 ![](https://box.kancloud.cn/351b82b2d42ca6ccd7949b700e415196_771x319.png) *** 這里舉個小實例: ~~~ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="loginServlet" method="post"> user:<input type="text" name="user"> password:<input type="password" name="password"> <input type="submit" value="Submit"> </form> </body> </html> ~~~ 運行服務器,在瀏覽器中打開這個網頁,開發者工具,提交表單,提示404(申請資源不存在),在network中查看請求及響應消息。其中我們可以發現有表單信息(Form data),里面有我們表單提交的數據,我們應該獲取它,然后到后臺數據庫查詢,如果存在該信息,允許登錄,其中獲取信息這件事現在loginServlet就可以幫我們完成。 ## POST和GET請求 post請求方式我們可以看到請求參數在請求體里面 將請求方式改為get后再運行觀察 ~~~ <form action="loginServlet" method="get"> user:<input type="text" name="user"> password:<input type="password" name="password"> <input type="submit" value="Submit"> </form> ~~~ 可以觀察到地址欄是這樣的:[http://localhost:8080/ykbbs/loginServlet?user=123&password=123](http://localhost:8080/ykbbs/loginServlet?user=123&password=123) **get請求把請求參數附著在url后面**,中間以"?”分割。 #### 使用GET方式傳遞參數 ①在瀏覽器地址欄中輸入某個URL地址或單擊網頁上的一個超鏈接時,瀏覽器發出的HTTP請求消息的請求方式為GET。 ②如果網頁中的表單元素的method屬性被設置為了“GET”,瀏覽器提交這個FORM表單時生成的HTTP請求消息的請求方式也為GET。 ③使用GET請求方式給WEB服務器傳遞參數的格式: http://www.neusoft.net/counter.jsp?name=yzn&password=123 ④使用GET方式傳送的數據量一般限制在1KB以下。 #### 使用POST方式傳遞參數 ①POST請求方式主要用于向WEB服務器端程序提交FORM表單中的數據。 ②POST方式將各個表單字段元素及其數據作為HTTP消息的實體內容發送給WEB服務器,傳送的數據量要比使用GET方式傳送的數據量大得多。 *** ## 如何在Servlet中獲取信息 這里我們可以新建一個Servlet(eclipse中直接有Servlet選項)。 ~~~ @WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("post"); } } ~~~ 在之前我們自己寫的Servlet當中,處理請求的service()方法,在這里其實也是,之所以能沒有出現service()方法,是因為HttpServlet這個類已經對原來不完整且代碼冗余的Servlet接口進行了實現和封裝。這里doGet和doPost分別對應接受get和post請求,方便、簡單。 這里可以對之前的表單進行驗證,看請求是否能夠發到對應的方法中。其中,方法的參數**HttpServletRequest request**和**HttpServletResponse response**封裝了**請求和響應**信息 #### 一.如何獲取請求信息 **HttpServletRequest**常用的方法: ①**String getParameter(String name)** --根據請求參數的名字,返回參數值,特別常用 ~~~ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user = request.getParameter("user"); String password = request.getParameter("password"); System.out.println(user+" "+password); } ~~~ ![](https://box.kancloud.cn/7025a37a514a3926e9ba5d0e9807d86e_753x538.png) ②**String[] getParameterValues(String name)** --根據請求參數的名字,返回請求參數對應的字符串數組(例如:一組復選框-->名字是一樣的) ~~~ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String intrests[] = request.getParameterValues("hobby"); for(String str:intrests) { System.out.println(str); } } ~~~ ③Map getParameterMap() --返回請求參數的鍵值對:key:參數名,value:參數值(String數組) ④Enumeration getParameterNames() --返回參數名對應的Enumeration對象(集合對象),類似于getInitParameterNames()方法 ⑤獲取請求的URI ⑥獲取請求方式 ⑦獲取請求字符串 ⑧獲取Servlet名 ~~~ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI().toString(); System.out.println(requestURI); String method = request.getMethod(); System.out.println(method); String queryString = request.getQueryString(); System.out.println(queryString); String servletPath = request.getServletPath(); System.out.println(servletPath); } ~~~ #### 二.如何獲取響應信息 **HttpServletResponse**常用的方法: ①getWriter()方法 --返回PrintWriter對象,調用這個對象的println()方法可以將信息直接打印在客戶的瀏覽器上 ~~~ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("hello..."); } ~~~ ②setContentType()方法 --設置響應的類型 ~~~ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/msword"); PrintWriter out = response.getWriter(); out.println("hello..."); } ~~~ ③getOutputStream()方法,文件下載時講解使用。
                  <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>

                              哎呀哎呀视频在线观看