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

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

tomcat如何進(jìn)行請(qǐng)求信息編碼

2019-11-14 09:30:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

我們知道網(wǎng)絡(luò)傳輸?shù)模际嵌M(jìn)制字節(jié)流,那么服務(wù)器如何編碼,怎么知道哪個(gè)字符集進(jìn)行編碼呢,那我們深入分析下tomcat連接,仔細(xì)探討下。 接下來(lái),我們看一下段代碼,這是一個(gè)很簡(jiǎn)單的表單。

<form action="demo01?name=中國(guó)" method="post"> <input type="text" name="name1" value="張三"/> <input type="submit" value="提交"/> </form>

controller中,我們直接用 HttpServletRequest,不用sPRing獲取參數(shù)。

@RequestMapping(value = "/demo01", method = RequestMethod.GET) public String dologin1(HttpServletRequest request) throws UnsupportedEncodingException { log.info(request.getCharacterEncoding()); log.info("name:中國(guó)" + request.getParameter("name")); log.info("name1:張三" + request.getParameter("name1")); return "login";}

運(yùn)行tomcat,結(jié)果如下,中文亂碼: 這里寫圖片描述 我們用fiddler查看請(qǐng)求的詳情: 這里寫圖片描述 我們來(lái)經(jīng)過(guò)測(cè)試下:

@Test public void test() throws UnsupportedEncodingException { String str = "中國(guó)"; byte[] bytes = str.getBytes("utf-8"); System.out.println(Hex.encodeHex(bytes)); System.out.println(new String(bytes, "iso8859-1")); String str1 = "張三"; byte[] bytes1 = str1.getBytes("utf-8"); System.out.println(Hex.encodeHex(bytes1)); System.out.println(new String(bytes1, "iso8859-1")); }

打印如下:

e4b8ade59bbdiso8859-1編碼: ??-???e5bca0e4b889?? ??‰

由此,可以發(fā)現(xiàn),我使用的谷歌瀏覽器,默認(rèn)使用的中文編碼為utf-8,而tomcat編碼默認(rèn)的是iso8859-1編碼,由于編碼對(duì)應(yīng)的字符不同,所以造成亂碼。 既然有編碼問(wèn)題,那么肯定可以解決,查看tomcat手冊(cè) 發(fā)現(xiàn)tomcat連接器可以指定uri編碼,參數(shù)URIEncoding:This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used. 在server.xml中配置如下:

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

此時(shí)運(yùn)行tomcat,uri參數(shù)問(wèn)題解決,結(jié)果如下: 這里寫圖片描述

那請(qǐng)求體參數(shù)如何進(jìn)行編碼呢?我們查看servelt源碼發(fā)現(xiàn),請(qǐng)求體的編碼可以在獲取參數(shù)前進(jìn)行設(shè)置,由此猜想,tomcat解析請(qǐng)求體參數(shù)是在第一次使用時(shí)進(jìn)行解析,也不難理解,字符串解析是耗性能的,既然不需要使用,那么不用解析,同樣就不用消耗這部分性能。

/** * 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(). Otherwise, it has no effect. * * @param env <code>String</code> containing the name of * the character encoding. * @throws UnsupportedEncodingException if this * ServletRequest is still in a state where a * character encoding may be set, but the specified * encoding is invalid */ public void setCharacterEncoding(String env) throws UnsupportedEncodingException;

改變controller代碼,增加utf-8編碼:

@RequestMapping(value = "/demo01", method = RequestMethod.POST) public String dologin(HttpServletRequest request) throws UnsupportedEncodingException { request.setCharacterEncoding("utf-8"); log.info(request.getCharacterEncoding()); log.info("name:中國(guó)" + request.getParameter("name")); log.info("name1:張三" + request.getParameter("name1")); return "login"; }

運(yùn)行tomcat,發(fā)現(xiàn)編碼問(wèn)題完美解決: 這里寫圖片描述 難道每次獲取參數(shù)前都要設(shè)置編碼嗎?肯定有更省事的方式,那就是過(guò)濾器,且我們可以直接用spring提供的現(xiàn)成的,org.springframework.web.filter.CharacterEncodingFilter,查看其代碼:

@Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); }

發(fā)現(xiàn)也就是設(shè)置request編碼而已,沒(méi)什么神秘的,不過(guò)既然有現(xiàn)成的,我們何必再造輪子 呢。 沒(méi)有從tomcat源碼中分析出問(wèn)題有些遺憾,查看了tomcat部分源碼,也沒(méi)得要領(lǐng),只能說(shuō)明功力還不夠,需要繼續(xù)精進(jìn),不過(guò)合理的推導(dǎo)也不失為解決問(wèn)題的一種好辦法。


上一篇:模板方法模式

下一篇:tjdsc

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南川市| 仙居县| 兴隆县| 台东市| 云龙县| 北京市| 台北县| 虞城县| 阿克陶县| 武穴市| 岗巴县| 镇远县| 襄垣县| 界首市| 商水县| 新龙县| 白玉县| 东乡县| 藁城市| 普格县| 嘉义市| 香河县| 陵水| 永福县| 营口市| 阆中市| 密云县| 靖安县| 兴业县| 丘北县| 奉贤区| 上犹县| 红安县| 内黄县| 广东省| 滦平县| 新建县| 黄龙县| 合山市| 仁布县| 宣汉县|