tomcat5的中文参数问题的解决方法

今天遇到了struts中传中文参数的问题:介绍一下具体情景先,中文将会是用户输入,然后通过javascript去取值后拼接网页地址,接着将它赋值给一个iframe的location.href,最后在struts的action中需要去取值.
解决办法如下:

1. jsp文件的第一句写上:
<%@ page contentType="text/html; charset=GBK"%>
然后就可以直接传递中文参数了

2. 添加filter功能:
2.1 写一个类实现javax.servlet.Filter,具体如下:
package cn.com.umessage.config;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServletFilterConfig implements Filter {

protected String encode;

public void init(FilterConfig filterConfig) throws ServletException {
this.encode = filterConfig.getInitParameter("encode");
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String encode = this.encode;
if (encode != null) {
request.setCharacterEncoding(encode);
}
chain.doFilter(request, response);
}

public void destroy() {
}

}
  2.2 修改web.xml
    <!-- servlet filter config -->
<filter>
<filter-name>encodeFilter</filter-name>
<filter-class>
cn.com.umessage.config.ServletFilterConfig
</filter-class>
<init-param>
<param-name>encode</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
3. 最后还有一步,一定不能忘,tomcat5需要再设置一下${TOMCAT_HOME}/conf/server.xml文件,在最后添加 URIEncoding="GBK"
    <Connector port="8080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK"/>

你可能感兴趣的:(JavaScript,tomcat,jsp,servlet,struts)