似乎Tomcat编码问题每个人起初都遇到过,而且是个很头疼的问题,获取的数据总是乱码。最早的时候是在action中写编码转换的方法,如今看来很繁琐的方式,在当时兴奋了好久,终于可以正常显示中文了:
编码转换
public String code(String s) {
String str = s;
try {
byte b[] = str.getBytes("ISO-8859-1");
str = new String(b);
return str;
} catch(Exception e) {
return str;
}
}
---------------------------------------------------------------------
之后才是用到这句是这个很无敌的语句:
request.setCharacterEncoding("UTF-8");
----------------------------------------------------------------------
最后才是最无敌的方法,过滤器:
1:在TOMCAT自带的例子中(\webapps\examples\WEB-INF\classes\filters)找到2个文件RequestDumperFilter.java,SetCharacterEncodingFilter.java,加入到工程文件中去。
2:配置 server.xml。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
加入红色的部门就好了。
3:配置 web.xml
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
编码转换
public String code(String s) {
String str = s;
try {
byte b[] = str.getBytes("ISO-8859-1");
str = new String(b);
return str;
} catch(Exception e) {
return str;
}
}
---------------------------------------------------------------------
之后才是用到这句是这个很无敌的语句:
request.setCharacterEncoding("UTF-8");
----------------------------------------------------------------------
最后才是最无敌的方法,过滤器:
1:在TOMCAT自带的例子中(\webapps\examples\WEB-INF\classes\filters)找到2个文件RequestDumperFilter.java,SetCharacterEncodingFilter.java,加入到工程文件中去。
2:配置 server.xml。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
加入红色的部门就好了。
3:配置 web.xml
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>