1)过滤器类一
package web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FilterTest implements Filter{
private FilterConfig filterConfig;
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy....");
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request=(HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse)arg1;
String name=(String)request.getParameter("name");
String param=filterConfig.getInitParameter("illegalStr");
System.out.println(param+"............");
if(name.equals("wangqisen")){
PrintWriter pw=response.getWriter();
pw.println("nameerror");
}else{
arg2.doFilter(arg0, arg1);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig=arg0;
}
}
2)过滤器类二
package web;
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FilterTest2 implements Filter{
private FilterConfig filterConfig;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req=(HttpServletRequest)arg0;
HttpServletResponse res=(HttpServletResponse)arg1;
String name=req.getParameter("name");
String forbbiden=filterConfig.getInitParameter("forbidden");
if(name.equals(forbbiden)){
System.out.println("forbidden error ............");
}else{
arg2.doFilter(arg0, arg1);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig=arg0;
}
}
3)在配置文件中添加过滤器。注意filter标签应该在servlet标签前面声明。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>filter1</filter-name>
<filter-class>web.FilterTest</filter-class>
<init-param>
<param-name>illegalStr</param-name>
<param-value>pig</param-value>
</init-param>
</filter>
<filter>
<filter-name>filter2</filter-name>
<filter-class>web.FilterTest2</filter-class>
<init-param>
<param-name>forbidden</param-name>
<param-value>zhangfei</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/filterTest</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filter2</filter-name>
<url-pattern>/filterTest</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>filterTest</servlet-name>
<servlet-class>web.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>filterTest</servlet-name>
<url-pattern>/filterTest</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>