不同的AppServer如何对URL进行编码实现的原理可能会不同.(Resin和Tomcat肯定会不同)
在Tomcat下至少做到了这四个步骤就可以解决中文编码的问题
(1)确定JSP页面头部是否有:<%@ page contentType="text/html; charset=GBK" %>
(2)用这个转码:
String param= new String(request.getParameter("param").getBytes("ISO-8859-1"), "GBK");
(3)添加filter字符过滤器
(4)如果是通过"a.jsp?param=中文"传递参数,则需要:
a.在传参数之前先把参数进行转码:java.net.URLEncoder.encode(param);
取值用java.net.URLDncoder.decode(param);再转回中文
b.在你的Tomcat目录-->conf目录-->server.xml里找出这段:
<Connector
port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" <!--在里边加上这个参数-->URIEncoding="gb2312"
/>
关键是第4步,添加URIEncoding="gb2312"
在Resin中,修改Resin.conf
<server>
......
<url-character-encoding>utf-8</url-character-encoding>
.......
</server>
因为,Resin decodes the URL before it dispatches to a <web- app> to the <url-character-encoding> needs to be at the <server> level.
在Tomcat下至少做到了这四个步骤就可以解决中文编码的问题
(1)确定JSP页面头部是否有:<%@ page contentType="text/html; charset=GBK" %>
(2)用这个转码:
String param= new String(request.getParameter("param").getBytes("ISO-8859-1"), "GBK");
(3)添加filter字符过滤器
(4)如果是通过"a.jsp?param=中文"传递参数,则需要:
a.在传参数之前先把参数进行转码:java.net.URLEncoder.encode(param);
取值用java.net.URLDncoder.decode(param);再转回中文
b.在你的Tomcat目录-->conf目录-->server.xml里找出这段:
<Connector
port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" <!--在里边加上这个参数-->URIEncoding="gb2312"
/>
关键是第4步,添加URIEncoding="gb2312"
在Resin中,修改Resin.conf
<server>
......
<url-character-encoding>utf-8</url-character-encoding>
.......
</server>
因为,Resin decodes the URL before it dispatches to a <web- app> to the <url-character-encoding> needs to be at the <server> level.