<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] ![](https://img.kancloud.cn/3d/10/3d100eab93d16758a2699d2a7d61d348_1125x347.png) # 1. out[javax.servlet.jsp.JspWriter] ## 問題01:如何使用out對象向網頁中輸出內容? ![](https://img.kancloud.cn/76/5e/765ee31e0bbf7786dcf95f958fde3eac_420x173.png) ## 問題02:JspWriter對象和PrintWriter對象有什么區別? ![](https://img.kancloud.cn/db/0e/db0e345cb91a6da49e63c3d2ff5e61e0_577x172.png) ![](https://img.kancloud.cn/e5/b7/e5b7d1853ee2b71af3fcef05826b7d40_657x427.png) ## 問題03:如何解決JspWriter輸出順序與代碼不一致的問題? > **設置緩沖區為0kb** ![](https://img.kancloud.cn/94/4f/944fda4ee5950f15fe98c5aae51038dc_828x287.png) # 2. request[javax.servlet.http.HttpServletRequest] | 分類 | 功能 | 應用 | | --- | --- | --- | | 行信息 | 獲取請求行(方式、協議、請求URI)| 限制請求方式和協議、轉換相對路徑| | 頭信息 | 獲取請求頭(客戶端IP、referer)| 限制請求客戶端、實現防盜鏈| | 主體 | 獲取請求參數|獲得單個值或者多個請求參數傳遞的值(單個、多個) | | 編碼 | 設置請求編碼| 防止請求參數的值發生亂碼【重要】(post亂碼、get亂碼)| | 視圖跳轉 | 請求轉發| 將請求遞交給下一個視圖,繼續進行處理(請求轉發、相對**web應用**路徑)| ## 問題04:如何獲取請求行信息?如何限制請求方式? * [ ] getMethod() 獲得客戶端向服務器端傳送數據的方法,如get,post * [ ] getProtocol()獲得客戶端向服務器端傳送數據所依據的協議名稱 ![](https://img.kancloud.cn/47/72/477240520b159c34e11afcdc0c5f8365_807x282.png) ![](https://img.kancloud.cn/91/87/918700fba5c6667fc3ee4fbeed50cda5_632x219.png) ## 問題05:如何轉換相對路徑? ``` String path = request.getContextPath(); ``` > Returns the portion of the request URI that indicates **the context of the request**. The context path always **comes first** in a request URI. The path **starts with** a "/" character but **does not end with** a "/" character. ![](https://img.kancloud.cn/26/18/261852607208f81c4ea0a3ba8af06f76_702x129.png) > **注意:得到的路徑最后沒有"/",需要手動添加。** ## 問題06:如何獲得其他頭信息? * [ ] getHeader(String name) 獲得HTTP協議定義的文件頭信息 * [ ] getRemoteAddr()獲取客戶端的IP地址 * [ ] getServerPort() 獲取服務器的端口號 ![](https://img.kancloud.cn/ca/b9/cab9eb666de4a3836c6b01eb85960c22_839x654.png) ## 問題07:如何實現防盜鏈? ![](https://img.kancloud.cn/08/4d/084d33cf1d62d893f5b5f0dea3c27782_592x416.gif) ![](https://img.kancloud.cn/31/30/31302e030e2f33340a2f95e87ea1e013_856x195.png) ## 問題08:如何獲得請求參數? ``` String username = request.getParameter("username"); ``` Returns the value of a request parameter as a **String**, or **null** if the parameter does **not exist**. You should only use this method when you are sure the parameter **has only one value**. If the parameter might have **more than one value**, use **getParameterValues**(java.lang.String). ``` String[] users = request.getParameterValues("user"); ``` Returns **an array of String objects** containing all of the values the given request parameter has, or null if the parameter does not exist. If the parameter has **a single value**, the array has **a length of 1**. ![](https://img.kancloud.cn/b4/d9/b4d9fecaeb0de864a6ee1864df4052bf_565x318.gif) > 數組的顯示可以使用:<%=Arrays.toString(字符串數組)%> ## 問題09:如何解決請求亂碼? * [ ] GET提交亂碼 ``` String username = request.getParameter("username"); username = new String(username.getBytes("ISO8859-1"),"UTF-8"); ``` * [ ] POST提交亂碼 ![](https://img.kancloud.cn/26/d5/26d5696227d8b9d78d4a7fd4e3723ae3_565x318.gif) ``` request.setCharacterEncoding("utf-8"); ``` > Overrides the name of the character encoding used in the body of this request. This method must be called **prior** to **reading request parameters** or **reading input** using getReader(). ## 問題10:如何進行視圖跳轉(請求轉發)? ``` request.getRequestDispatcher("URL").forward(request, response); ``` > Returns a RequestDispatcher object that acts as a wrapper for **the resource located at the given path**. The pathname specified **may be relative**, although it **cannot** extend **outside the current servlet context**. If the path **begins with a "/"** it isinterpreted as relative to the **current context root**. > Forwards a request from a servlet **to another resource** (servlet, JSP file, or HTML file) on the server. This method allows one servlet **to do preliminary processing** of a request and **another resource to generate the response. **forward** should be called before the response has been committed** to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. ![](https://img.kancloud.cn/69/e1/69e1970aca4c8449ba1e5d7a927aa8be_447x199.png) # 3. response[javax.servlet.http.HttpServletResponse] | 分類 | 功能 | 應用 | | --- | --- | --- | | 行信息 | 設置響應行(狀態碼)| 設置正確狀態碼、設置異常狀態碼| | 頭信息 |設置響應頭(refresh、禁止緩存) | 可以實現自動刷新和跳轉、實現禁止緩存| | 主體 | 獲取out并輸出內容|獲取out對象,輸出內容print、append | | 編碼 | 設置響應編碼| 設置響應內容類型以及編碼(通過header設置、直接設置)| | 視圖跳轉 | 重定向|定向到其他視圖(重定向、相對**服務器**路徑)| ## 問題11:如何設置響應狀態碼?顯示錯誤消息頁面 * [ ] setStatus(int status) ![](https://img.kancloud.cn/cb/e9/cbe9644e6e9cceb27b916bb7e7845f3b_401x178.png) > **void javax.servlet.http.HttpServletResponse.setStatus(int sc)** > **Sets the status code** for this response. This method is used to set the return status code when **there is no error** (for example, for the status codes SC_OK or SC_MOVED_TEMPORARILY). If **there is an error**, and the caller wishes to **invoke an error-page** defined in the web application, the **sendError** method should be used instead. ## 問題12:如何手動喚醒錯誤處理頁面? ![](https://img.kancloud.cn/ab/f9/abf93cb58ce357049447a1fa49558097_779x453.png) ![](https://img.kancloud.cn/22/9a/229a2d0bfa3c0554d7317a63c124e7e6_1042x246.png) ## 問題13:如何實現網頁自動跳轉或刷新? * [ ] setHeader ![](https://img.kancloud.cn/47/78/4778f3e347bd5e033c0f2a3c4e049c08_313x196.gif) ![](https://img.kancloud.cn/68/8b/688be66a9a62e2874dd4cd044d728951_1108x190.png) ![](https://img.kancloud.cn/83/00/83008309f144fb20e66167b7289e0bff_545x340.gif) ``` response.setHeader("refresh", "5;url='http://www.pzhu.cn'"); ``` ## 問題14:如何實現網頁禁止緩存? ``` response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); ``` ## 問題15:如何使用response實現輸出內容? ``` response.getWriter().print(""); ``` ![](https://img.kancloud.cn/20/8f/208f965922b5215bd98f616cda44347d_630x246.png) ## 問題16:如何解決響應內容出現亂碼? ``` response.setCharacterEncoding("UTF-8"); ``` ![](https://img.kancloud.cn/b3/83/b38308a6575a5544ffc5c2b00efd5eec_602x248.png) ## 問題17:為什么設置了編碼,前端頁面還是亂碼?嘗試切換瀏覽器編碼? > **void javax.servlet.ServletResponse.setCharacterEncoding(String charset)** > **In the case of HTTP**, the character encoding is communicated **as part of the Content-Type header** for text media types. Note that the character encoding **cannot be communicated** via HTTP headers if **the servlet does not specify a content type**; however, it is still used to encode text written via the servlet response's writer. ![](https://img.kancloud.cn/6b/34/6b34fe03272138c285a27b7ea2d86161_483x302.png) > **void javax.servlet.ServletResponse.setCharacterEncoding(String charset)** **【解決辦法】** > **Sets the character encoding (MIME charset)** of the response being sent tothe client, for example, to UTF-8. If the character encoding has already been set by setContentType or setLocale, this method overrides it. Calling **setContentType** with the String of **text/html** and calling **this method** with the String of **UTF-8** is equivalent withcalling **setContentType** with the String of **text/html; charset=UTF-8**. > This method can be called repeatedly to change the character encoding.This method has **no effect** if it is called **after getWriter** has been called or after **the response has been committed**. ![](https://img.kancloud.cn/39/f6/39f607bab526b94f338fd181cb60ecf4_675x319.png) ## 問題18:如何實現重定向? ``` response.sendRedirect("URL"); ``` > Sends a temporary **redirect** response to the client using the specified redirect location **URL**. This method can **accept relative URLs**; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative **without a leading '/'** the container interprets it as relative to **the current request URI**. If the location is relative **with a leading '/' the** container interprets it as relative to **the servlet container root**. ![](https://img.kancloud.cn/c7/11/c711d09e0224fafb2fa975099b3d5d7d_468x279.png) * [ ] 注意:多次重定向可能產生以下異常 ![](https://img.kancloud.cn/11/69/1169f85517034aa95d002be73538e6bc_991x584.png) ## 問題19:請求轉發和重定向有什么區別? ![](https://img.kancloud.cn/2d/3f/2d3f2986500f566b720d546ae903040c_700x387.png) 優點:可以轉到服務器內部路徑或不可直接訪問路徑,隱藏目標資源路徑。 缺點:可能發生相對路徑錯誤。 * [ ] 程序發生異常喚醒錯誤頁面也是屬于請求轉發的一種 * [ ] 當在錯誤頁面存在相對路徑時就可能出現異常 ![](https://img.kancloud.cn/8f/2f/8f2f4d136cbb95ed18a547c4650fb77f_690x149.png) ![](https://img.kancloud.cn/da/e4/dae4a9769b10e67912dc763ce98bf134_901x294.png)
                  <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>

                              哎呀哎呀视频在线观看