配置一个filter,
SetCodeFilter 源代码:
package com.itcast.filter;
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 SetCodeFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding("utf8");
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
web.xml文件配置:、
<filter>
<filter-name>setCodeFilter</filter-name>
<filter-class>com.itcast.filter.SetCodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>setCodeFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>setCodeFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>