10,606
社区成员
发帖
与我相关
我的任务
分享
[code=text]<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
[/code]
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("utf-8");
arg1.setCharacterEncoding("utf-8");
arg2.doFilter(arg0, arg1);
}
看 HttpServletRequest的setCharacterEncoding方法的描述
Overrides the name of the character encoding used in the body of this request.
是说请求的内容体采用指定的charset,而http get方法的内容体是空的,它的请求参数是附加上URL上,而post方法的内容体存在请求参数,内容是key=value这种键值对
2.tomcat的Connector
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.
useBodyEncodingForURI属性
1) This setting is applied only to the query string of a request. Unlike URIEncoding it does not affect the path portion of a request URI.
2) If request character encoding is not known (is not provided by a browser and is not set by SetCharacterEncodingFilter or a similar filter using Request.setCharacterEncoding method), the default encoding is always "ISO-8859-1". The URIEncoding setting has no effect on this default.
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="80"
protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="443"
compression="on"
compressionMinSize="2048"
URIEncoding="UTF-8"
noCompressionUserAgents="gozilla,traviata"
compressableMimeType="text/html,text/xml,application/javascript,text/css,text/plain,text/json,application/json" secure="true"/>
<form method="GET" action="/a">
<input type="text" name="nick" value="中文" />
</form>
提交后:/a?nick=中文,就乱码