java學習筆記—HttpServletResponse(22)

來源:互聯網
上載者:User

標籤:

1  亂碼處理

GET請求使用直接通過地址欄斷行符號或超連結的單擊以及form表單中method的get資料提交過來的請求,該請求和傳遞使用者參數的方式是http://www.jnb.com?name=jack&age=36 因此可見get方式的請求參數有限(小於1K)。而且傳遞的資料直接在地址欄可見。如: 百度的搜尋索引鍵。

1.1      編寫一個提交get資料的一個註冊表單

 

 <form action="/day07/regist" method="get">       <table align="center" border="1">         <tr>           <td>使用者:</td>           <td><input type="text" name="uname"/></td>         </tr>         <tr>           <td>地址:</td>           <td><input type="text" name="address"/></td>         </tr>         <tr align="center">           <td colspan="2">               <input type="submit" value="註冊"/>               <input type="reset" value="重設"/>           </td>         </tr>       </table>    </form>

2 編寫一個RegistServlet處理使用者的Get請求資料

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        // 處理響應資料的格式和編碼        response.setCharacterEncoding("utf-8");        response.setContentType("text/html;charset=utf-8");        // 擷取字元輸出資料流對象        PrintWriter out = response.getWriter();        // 擷取請求參數        String name = request.getParameter("uname");        String address = request.getParameter("address");        // 輸出資料        out.println("name="+name);        out.println("<br/>");        out.println("address="+address);    }

運行結果發現輸入中文提交後顯示結果為亂碼:

3    分析亂碼的原因

 

4 使用代碼解決亂碼問題

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        // 處理響應資料的格式和編碼        response.setCharacterEncoding("utf-8");        response.setContentType("text/html;charset=utf-8");        // 擷取字元輸出資料流對象        PrintWriter out = response.getWriter();        // 擷取請求參數        String name = request.getParameter("uname");        String address = request.getParameter("address");        // 擷取ISO8859-1的未經處理資料        byte [] bs = name.getBytes("ISO8859-1");        name = new String(bs,"UTF-8");        bs = address.getBytes("ISO8859-1");        address = new String(bs,"UTF-8");        // 輸出資料        out.println("name="+name);        out.println("<br/>");        out.println("address="+address);    }

如果使用以上的代碼解決GET亂碼每一個請求參數都需要重新的解碼太繁瑣,因此可以直接使用伺服器通知瀏覽器以指定的方式進行編碼字元資料。

5 使用URIEnoding方式解決GET亂碼問題

<Connector port="8080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443"                URIEncoding="utf-8"/>

URIEncoding主要指定的是%E5%8D%A1%E5%8D%A1的資料的解碼方式,如果沒有指定那麼預設是ISO8859-1,如果指定那麼採用指定的碼錶進行解碼。因此伺服器端就沒有必要使用兩個中編碼方式進行轉碼了。

注意:在實際的生產環境中我們代碼的亂碼問題不可能以來伺服器協助我們解決,因此推薦使用第一個轉碼方式。後期可以直接實現一個亂碼的過濾器實現處理亂碼的問題。

  1. POST請求

POST請求主要是以form表單的method=”post”發送的請求。該請求的資料在HTTP協議的請求體中。可以傳遞任意大小的資料。因此比較適合上傳檔案。

修改以上的註冊頁面提交方式為post,那麼在doPost()直接擷取資料並輸出同樣出現亂碼。

使用代碼解決亂碼

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        // 處理響應資料的格式和編碼        response.setCharacterEncoding("utf-8");        response.setContentType("text/html;charset=utf-8");        // 處理post請求參數亂碼        request.setCharacterEncoding("utf-8");        // 擷取字元輸出資料流對象        PrintWriter out = response.getWriter();        // 擷取請求參數        String name = request.getParameter("uname");        String address = request.getParameter("address");        // 輸出資料        out.println("name="+name);        out.println("<br/>");        out.println("address="+address);}

總結:以後在實際的開發中避免不了給頁面輸出中文資料以及中文的參數。那麼以後在處理使用者請求的doGet和doPost方法中我們應直接編寫如下三句:

response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");

 

java學習筆記—HttpServletResponse(22)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.