java 编码设置

public class EncodingFilter implements Filter {

private FilterConfig filterConfig=null;

public void destroy() {
  this.filterConfig=null;
}

public void doFilter(
   ServletRequest request,
   ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  request.setCharacterEncoding(this.filterConfig.getInitParameter("encoding"));
  response.setCharacterEncoding(this.filterConfig.getInitParameter("encoding"));
  chain.doFilter(request, response);
}

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

}
下面是在web.xml的设置:
<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>com.emp.filter.EncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
  </init-param>
</filter>
这里我用和你一样的GBK

如果你用的是struts2:
那只要在他的配置文件struts.xml里面写:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 中文编码处理 -->
<constant name="struts.i18n.encoding" value="GBK"></constant>

<!-- 以下配置我省略了 -->
。。。。。。。

</struts>
这里我也是用GBK,你自己看着弄上去

你可能感兴趣的:(java,struts,struts2)