|
在实体类中:
package guolv;
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 Myfilter implements Filter { //建立以和类 继续Filter接口
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
try {
arg0.setCharacterEncoding("gb2312");//设置哀求回复 的文字格局
arg1.setCharacterEncoding("gb2312");
}
catch (RuntimeException e)
{
e.printStackTrace();
}
arg2.doFilter(arg0,arg1); //设置 哀求回复都可以
}
public void init(FilterConfig arg0) throws ServletException {
}
}
在web.xml文件中:
<filter>
<filter-name>test</filter-name>
<filter-class>guolv.Myfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>test</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> |
|