麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

tomcat如何進行請求信息編碼

2019-11-14 08:54:44
字體:
來源:轉載
供稿:網友

我們知道網絡傳輸的,都是二進制字節流,那么服務器如何編碼,怎么知道哪個字符集進行編碼呢,那我們深入分析下tomcat連接,仔細探討下。 接下來,我們看一下段代碼,這是一個很簡單的表單。

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

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

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

運行tomcat,結果如下,中文亂碼: 這里寫圖片描述 我們用fiddler查看請求的詳情: 這里寫圖片描述 我們來經過測試下:

@Test public void test() throws UnsupportedEncodingException { String str = "中國"; 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?? ??‰

由此,可以發現,我使用的谷歌瀏覽器,默認使用的中文編碼為utf-8,而tomcat編碼默認的是iso8859-1編碼,由于編碼對應的字符不同,所以造成亂碼。 既然有編碼問題,那么肯定可以解決,查看tomcat手冊 發現tomcat連接器可以指定uri編碼,參數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"/>

此時運行tomcat,uri參數問題解決,結果如下: 這里寫圖片描述

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

/** * 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:中國" + request.getParameter("name")); log.info("name1:張三" + request.getParameter("name1")); return "login"; }

運行tomcat,發現編碼問題完美解決: 這里寫圖片描述 難道每次獲取參數前都要設置編碼嗎?肯定有更省事的方式,那就是過濾器,且我們可以直接用spring提供的現成的,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); }

發現也就是設置request編碼而已,沒什么神秘的,不過既然有現成的,我們何必再造輪子 呢。 沒有從tomcat源碼中分析出問題有些遺憾,查看了tomcat部分源碼,也沒得要領,只能說明功力還不夠,需要繼續精進,不過合理的推導也不失為解決問題的一種好辦法。


上一篇:骨牌鋪方格

下一篇:Spring

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美精品亚洲人成在线观看 | 黄色网址在线免费 | 国产精品久久久久久久久久电影 | 日韩视频在线观看免费视频 | 超碰人人做人人爱 | 欧美特黄一级高清免费的香蕉 | 国产精品69页 | 色中色综合网 | 国产亚洲欧美在线视频 | 成人一区二区三区四区 | 国产亚洲精品综合一区91 | a网在线 | 羞羞视频免费视频欧美 | 日韩精品久久久久久久电影99爱 | 色妞妞视频 | 成人福利视频在线 | 久久国产夫妻视频 | 中文字幕国产日韩 | 久久精品观看 | 一级黄色免费大片 | 一级黄色大片在线观看 | 国产一区二区免费在线观看 | 国产精品免费久久久久 | 久久91久久久久麻豆精品 | 国产成人小视频在线观看 | 毛片网站网址 | 免费a级毛片永久免费 | 天天草天天爱 | 污污黄| 69性欧美高清影院 | 国产91小视频在线观看 | 亚洲一区二区三区视频 | 免费黄网站在线播放 | 欧美在线一级 | 猫咪av在线 | 久色视频网站 | 久久艹精品 | 91网站在线观看视频 | 久草视频国产在线 | 亚洲性生活免费视频 | 亚洲免费看片网站 |