国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

form提交數(shù)據(jù)中文亂碼問題總結(jié)

2019-11-14 21:38:59
字體:
供稿:網(wǎng)友
form提交數(shù)據(jù)中文亂碼問題總結(jié)

一: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")先編碼。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁强县| 广饶县| 呼和浩特市| 宿松县| 阳城县| 报价| 姜堰市| 抚宁县| 江山市| 锦屏县| 新龙县| 黄平县| 大同市| 沙雅县| 商丘市| 华安县| 泽普县| 奉化市| 老河口市| 依兰县| 绿春县| 龙胜| 金湖县| 星子县| 边坝县| 盈江县| 资阳市| 阿坝| 剑阁县| 定西市| 东乌| 五常市| 武清区| 临城县| 湖南省| 长武县| 绵竹市| 海原县| 石林| 化州市| 革吉县|