如何在Tomcat中设置请求编码? [英] How to set request encoding in Tomcat?
本文介绍了如何在Tomcat中设置请求编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的Java webapp中有问题。
这是index.jsp中的代码:
<%@ page contentType =text / htmlpageEncoding =UTF-8%>
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN
http://www.w3.org/TR/html4/loose.dtd\">
<%request.setCharacterEncoding(UTF-8);
response.setCharacterEncoding(UTF-8);
%>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title> JSP页面< / title>
< / head>
< body>
< h1> Hello World!< / h1>
< form action =index.jspmethod =get>
< input type =textname =q/>
< / form>
Res:<%= request.getParameter(q)%>
< / body>
< / html>
当我连线请求时,浏览器发送此标题:
GET /kjd/index.jsp?q=%C3%A9 HTTP / 1.1\r\\\
...
接受-Charset:UTF-8,* \r\\\
Tomcat服务器返回这个:
Content-Type:text / html; charset = UTF-8 \r\\\
但是如果我在我的表单中发送é(UTF-8中的%C3%A9)
我理解的是浏览器发送一个用UTF-8编码的é(%C3%A9)。
但服务器将此解释为ISO-8859-1。因此,%C3解码为Ã,%A9为©,然后发送回以UTF-8编码的响应。
在代码中,请求应该被解码with UTF-8:
request.setCharacterEncoding(UTF-8);但是,如果我发送这个url:
pre> http:// localhost:8080 / kjd / index.jsp?q =%E9
%E9被ISO-8859-1打乱,并显示é。
为什么这不工作?为什么请求使用ISO-8859-1解码?
我在Tomcat 6和7,Windows和Ubuntu上尝试过。
解决方案
request.setCharacterEncoding(UTF-8);
仅设置请求的编码正文(由POST请求使用),而不是请求 URI (GET请求已使用)的编码。
您需要将 URIEncoding
属性设置为 UTF-8
c和Tomcat的 /conf/server.xml
的
< Connector ... URIEncoding =UTF-8>
或确保使用与正文相同的编码解析URI:
< Connector ... useBodyEncodingForURI =true>
另请参阅:
请删除这些