1 亂碼處理
GET請(qǐng)求使用直接通過(guò)地址欄回車或超鏈接的單擊以及form表單中method的get數(shù)據(jù)提交過(guò)來(lái)的請(qǐng)求,該請(qǐng)求和傳遞用戶參數(shù)的方式是http://www.jnb.com?name=jack&age=36 因此可見(jiàn)get方式的請(qǐng)求參數(shù)有限(小于1K)。而且傳遞的數(shù)據(jù)直接在地址欄可見(jiàn)。如: 百度的搜索關(guān)鍵字。
1.1 編寫(xiě)一個(gè)提交get數(shù)據(jù)的一個(gè)注冊(cè)表單
<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="注冊(cè)"/> <input type="reset" value="重置"/> </td> </tr> </table> </form>
2 編寫(xiě)一個(gè)RegistServlet處理用戶的Get請(qǐng)求數(shù)據(jù)
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 處理響應(yīng)數(shù)據(jù)的格式和編碼 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // 獲取字符輸出流對(duì)象 PRintWriter out = response.getWriter(); // 獲取請(qǐng)求參數(shù) String name = request.getParameter("uname"); String address = request.getParameter("address"); // 輸出數(shù)據(jù) out.println("name="+name); out.println("<br/>"); out.println("address="+address); }運(yùn)行結(jié)果發(fā)現(xiàn)輸入中文提交后顯示結(jié)果為亂碼:
3 分析亂碼的原因

4 使用代碼解決亂碼問(wèn)題
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 處理響應(yīng)數(shù)據(jù)的格式和編碼 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // 獲取字符輸出流對(duì)象 PrintWriter out = response.getWriter(); // 獲取請(qǐng)求參數(shù) String name = request.getParameter("uname"); String address = request.getParameter("address"); // 獲取ISO8859-1的原始數(shù)據(jù) byte [] bs = name.getBytes("ISO8859-1"); name = new String(bs,"UTF-8"); bs = address.getBytes("ISO8859-1"); address = new String(bs,"UTF-8"); // 輸出數(shù)據(jù) out.println("name="+name); out.println("<br/>"); out.println("address="+address); }如果使用以上的代碼解決GET亂碼每一個(gè)請(qǐng)求參數(shù)都需要重新的解碼太繁瑣,因此可以直接使用服務(wù)器通知瀏覽器以指定的方式進(jìn)行編碼字符數(shù)據(jù)。
5 使用URIEnoding方式解決GET亂碼問(wèn)題
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>
URIEncoding主要指定的是%E5%8D%A1%E5%8D%A1的數(shù)據(jù)的解碼方式,如果沒(méi)有指定那么默認(rèn)是ISO8859-1,如果指定那么采用指定的碼表進(jìn)行解碼。因此服務(wù)器端就沒(méi)有必要使用兩個(gè)中編碼方式進(jìn)行轉(zhuǎn)碼了。
注意:在實(shí)際的生產(chǎn)環(huán)境中我們代碼的亂碼問(wèn)題不可能以來(lái)服務(wù)器幫助我們解決,因此推薦使用第一個(gè)轉(zhuǎn)碼方式。后期可以直接實(shí)現(xiàn)一個(gè)亂碼的過(guò)濾器實(shí)現(xiàn)處理亂碼的問(wèn)題。
POST請(qǐng)求主要是以form表單的method=”post”發(fā)送的請(qǐng)求。該請(qǐng)求的數(shù)據(jù)在HTTP協(xié)議的請(qǐng)求體中。可以傳遞任意大小的數(shù)據(jù)。因此比較適合上傳文件。
修改以上的注冊(cè)頁(yè)面提交方式為post,那么在doPost()直接獲取數(shù)據(jù)并輸出同樣出現(xiàn)亂碼。

使用代碼解決亂碼
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 處理響應(yīng)數(shù)據(jù)的格式和編碼 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // 處理post請(qǐng)求參數(shù)亂碼 request.setCharacterEncoding("utf-8"); // 獲取字符輸出流對(duì)象 PrintWriter out = response.getWriter(); // 獲取請(qǐng)求參數(shù) String name = request.getParameter("uname"); String address = request.getParameter("address"); // 輸出數(shù)據(jù) out.println("name="+name); out.println("<br/>"); out.println("address="+address);}總結(jié):以后在實(shí)際的開(kāi)發(fā)中避免不了給頁(yè)面輸出中文數(shù)據(jù)以及中文的參數(shù)。那么以后在處理用戶請(qǐng)求的doGet和doPost方法中我們應(yīng)直接編寫(xiě)如下三句:
response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注