Tomcat
tomcat_8 以前的版本编码格式为iso-8859-1 get请求中文乱码,需要在tomcat的server.xml文件中增加UTF-8 编码,
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
tomcat_8 编码格式是 UTF-8 所以不需要修改
Tomcat真正跑的目录
validateJarFile(/Users/wenyunli/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/biz-oss-base
1:表单提交controller获得中文参数后乱码解决方案
注意: jsp页面编码设置为UTF-8
form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果
[html] view plain copy
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
[html] view plain copy
- <form action="${ctx}/user/addUser" name="userForm" method="post">
修改web.xml,增加编码过滤器,如下(注意,需要设置forceEncoding参数值为true)
[html] view plain copy
- <filter>
- <filter-name>characterEncodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>characterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
截图
tomcat 8
https://tomcat.apache.org/tomcat-8.0-doc/config/http.html
This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.
tomcat 7
tomcat的 post和get解码方式是不一致的, 所以post没问题,保持编码和解码一致就行了,
不要使用这种方式对参数进行编码 new String(value.getBytes(“ISO-8859-1”), param); Tomcat7对URI默认编码是ISO-8859-1,Tomcat8对URI默认编码是UTF-8
使用new String(value.getBytes(“ISO-8859-1”), param);这种方式对tomcat7 可以解决get乱码问题, 如果迁移到tomcat8就有问题了
正确的行为:server.xml配置上URIEncoding=“UTF-8”
tomcat中,保证get数据采用UTF8编码,在server.xml中进行了如下设置:
加:URIEncoding="UTF-8"
<Connector port="8080" maxThreads="150"minSpareThreads="25"
maxSpareThreads="75" enableLookups="false"redirectPort="8443"
acceptCount="100" debug="99" connectionTimeout="20000"
disableUploadTimeout="true"URIEncoding="UTF-8"/>
指定了get时候的数据编码。当使用IIS作为webserver转发servlet/jsp请求给Tomcat时候,这个设置却失效
其实原因很简单:IIS是通过AJP协议,把请求转发到Tomcat监听的8009端口上的,所以这里针对8080的设置自然就无效
正确的方法是进行下面的设置:
<Connector port="8009" enableLookups="false"redirectPort="8443"
debug="0" protocol="AJP/1.3"URIEncoding="UTF-8"/>
查看8080端口的进程:
lsof -i:8080
杀进程
kill -9 PID