eclipse3.1+myeclipse4.1+tomcat5
在web.xml中加入
<filter>
<filter-name> SetCharacterEncoding </filter-name>
<filter-class> com.talkweb.tb.filter.SetCharacterEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> GBK </param-value>
</init-param>
<init-param>
<param-name> enable </param-name>
<param-value> true </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> SetCharacterEncoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
对应的filter代码为
public class SetCharacterEncodingFilter implements Filter {
protected FilterConfig filterConfig;
protected String encodingName;
protected boolean enable;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
loadConfigParams();
}
private void loadConfigParams() {
// encoding
this.encodingName = this.filterConfig.getInitParameter( "encoding ");
// filter enable flag...
String strIgnoreFlag = this.filterConfig.getInitParameter( "enable ");
if (strIgnoreFlag.equalsIgnoreCase( "true ")) {
this.enable = true;
} else {
this.enable = false;
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (this.enable) {
request.setCharacterEncoding(this.encodingName);
}
chain.doFilter(request, response);
}
public void destroy() {
}
}
页面如下
<%@ page contentType= "text/html; charset=GBK " %>