首先写一个类:
import java.io.IOException;
import javax.servlet.*;
public class setChangeCharsetFilter implements Filter {
protected String encoding = null;/要制定的编码,在web.xml中配置
protected FilterConfig filterConfig = null;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (request.getCharacterEncoding() == null){
String encoding = getEncoding();得到指定的编码名字
if (encoding != null)
request.setCharacterEncoding(encoding);设置request的编码
}
chain.doFilter(request, response);///有机会执行下一个filter
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");///得到在web.xml中配置的编码
}
protected String getEncoding() {
return (this.encoding);///得到指定的编码
}
}
然后在web.xml文档中写一个段: <filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>com.jivesoftware.forum.setChangeCharsetFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
就可以了。我在编译jive2.6的时候用到了!