一:form在前臺(tái)以post方式提交數(shù)據(jù):
瀏覽器將數(shù)據(jù)(假設(shè)為“中國”)發(fā)送給服務(wù)器的時(shí)候,將數(shù)據(jù)變成0101的二進(jìn)制數(shù)據(jù)(假設(shè)為98 99)時(shí)必然要查碼表,瀏覽器以哪個(gè)碼表打開網(wǎng)頁,瀏覽器就以哪個(gè)碼表提交數(shù)據(jù)。數(shù)據(jù)到達(dá)服務(wù)器后,數(shù)據(jù)(98 99)要封裝到request中,在servlet中調(diào)用Request的getParameter方法返回的是字符串(“中國”),方法內(nèi)部拿到數(shù)字后要轉(zhuǎn)成字符,一定要查碼表,由于request的設(shè)計(jì)者是外國人,所以默認(rèn)查的是他們常用的ISO8859-1,這就是請求數(shù)據(jù)產(chǎn)生亂碼的根源。
package com.yyz.request;import java.io.IOException;import java.io.PRintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//以post方式提交表單public class RequestDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //請求數(shù)據(jù)的中文亂碼問題 request.setCharacterEncoding("UTF-8");//客戶端網(wǎng)頁我們控制為UTF-8 String username = request.getParameter("username"); //獲取數(shù)據(jù)正常,輸出數(shù)據(jù)時(shí)可以查閱不同碼表 response.setCharacterEncoding("gb2312");//通知服務(wù)器發(fā)送數(shù)據(jù)時(shí)查閱的碼表 response.setContentType("text/html;charset=gb2312");//通知瀏覽器以何種碼表打開 PrintWriter out = response.getWriter(); out.write(username);} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response);}}
二:form在前臺(tái)以get方式提交數(shù)據(jù):
get方式提交的數(shù)據(jù)依然是瀏覽器用什么碼表打開就用什么碼表發(fā)送。不同的是,以get方式提交數(shù)據(jù)時(shí),request設(shè)置編碼無效。即使設(shè)置了UTF-8還是會(huì)去查ISO8859-1。得到(? ?),要解決這個(gè)問題,需要拿著(??)反向查ISO8859-1,拿到(98 99)后,再去查正確碼表。
1 package com.yyz.request; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 //以get方式提交表單11 public class RequestDemo extends HttpServlet {12 13 public void doGet(HttpServletRequest request, HttpServletResponse response)14 throws ServletException, IOException {15 //請求數(shù)據(jù)的中文亂碼問題16 request.setCharacterEncoding("UTF-8");//以get方式提交數(shù)據(jù)時(shí),request設(shè)置編碼無效。即使設(shè)置了UTF-8還是會(huì)去查ISO8859-117 String username = request.getParameter("username");18 System.out.println(username);19 byte source [] = username.getBytes("iso8859-1");20 username = new String (source,"UTF-8");21 System.out.println(username);22 23 } 24 25 public void doPost(HttpServletRequest request, HttpServletResponse response)26 throws ServletException, IOException {27 doGet(request,response);28 }29 30 }三:提交數(shù)據(jù)中文亂碼問題總結(jié):
1.如果提交方式為post,想不亂碼,只需要設(shè)置request對象的編碼即可。
注意:客戶機(jī)數(shù)據(jù)是以哪種方式提交的,request就應(yīng)該設(shè)成什么編碼。
2.如果提交方式為get,設(shè)置request對象的編碼是無效的,想不亂碼,只能手工轉(zhuǎn)換。
Stringdata ="???????";//亂碼字符串 byte source [] = data.getBytes("iso8859-1");//得到客戶機(jī)提交的原始數(shù)據(jù)data = new String (data.getBytes("iso8859-1"),"UTF-8");//解決亂碼
//等同于
data = new String (source,"UTF-8");
3.get方式的亂碼,還可以通過更改服務(wù)器配置的方式實(shí)現(xiàn)。更改Tomact的conf目錄下的server.xml文件。
3.1
這種方式并不推薦,因?yàn)楦牧朔?wù)器且并不靈活。
3.2
這么設(shè)置后,request的setCharacterEncoding設(shè)置什么編碼,連接器就用什么編碼,雖然比上一種更改靈活,但依然會(huì)導(dǎo)致我們的應(yīng)用程序牢牢依賴于服務(wù)器,也不被推薦。
四:最后的最后,提一個(gè)小細(xì)節(jié):URL地址后面如果跟了中文數(shù)據(jù),一定要經(jīng)過URL編碼。表單提交的參數(shù)有中文數(shù)據(jù),瀏覽器會(huì)自動(dòng)幫我們編碼,但如果是通過鏈接直接帶中文參數(shù),瀏覽器是不會(huì)幫我們編碼的,這時(shí)想通過上述第二種方式解決中文亂碼問題就時(shí)靈時(shí)不靈了,應(yīng)該通過URLEncoding.encode(,"UTF-8")先編碼。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注