当前位置: 代码迷 >> J2EE >> 啊该死的乱码
  详细解决方案

啊该死的乱码

热度:79   发布时间:2016-04-22 02:20:32.0
求救啊,该死的乱码
jsp页面form表单数据post到Action里面,Action里面使用sysout(request.getParameter("xxx"))在控制台上输出是乱码。
jdk:1.5
jsp:
HTML code
contentType="text/html; charset=GBK" pageEncoding="GBK"java文件编码:GBK

过滤器:
Java code
public void doFilter(ServletRequest request, ServletResponse response,            FilterChain chain) throws IOException, ServletException {        request.setCharacterEncoding("GBK");        chain.doFilter(request, response);    }

因为是jdk1.5没有response.setcharactereccoding方法

------解决方案--------------------
配置TOMCAT server.xml
2个位置加上
URIEncoding="GBK"
如下:
<Connector URIEncoding="GBK" useBodyEncoding="true" port="8080" protocol="HTTP/1.1" 
connectionTimeout="20000" 
redirectPort="8443" />
<Connector URIEncoding="GBK" useBodyEncoding="true" port="8009" protocol="AJP/1.3" redirectPort="8443" />
------解决方案--------------------
如果你用的是SSH2的话,你在web.xml中添加:

XML code
<filter>        <filter-name>encodingFilter</filter-name>        <filter-class>            org.springframework.web.filter.CharacterEncodingFilter        </filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>GBK</param-value>        </init-param>        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>
------解决方案--------------------
字符转换一下
String str = new String(request.getAttribute("元素名").getBytes("ISO8859_1"),"GBK");
------解决方案--------------------
在struts.xml中配置i18n
<constant name="struts.i18n.encoding" value="GB18030"></constant>
  相关解决方案