</init-param>
</filter>
<filter-mapping>
<filter-name>firstfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中,url-pattern表示对哪个页面进行过滤,如果全部过滤,则“/*” 此时在浏览器无法看到虚拟目录下的文件。
如果过滤器要将内容传递到目的地,需要FilterChain参数,将请求继续向下转发(此时又能看到目录):
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException,ServletException{
chain.doFilter(request, response);
}
chain.doFilter(request, response)该语句有两个功能:
1)将请求继续向下转发,如果还有别的过滤器则执行下一个过滤器
2)如没有下一个过滤器,则转向客户端请求的页面
每次请求过滤器会执行两次:FilterChain之前一次,之后一次
案例一:过滤文字
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException,ServletException {
String content=request.getParameter("content");
if(content!=null)
{
if(content.indexOf("AAA")==-1)
{
chain.doFilter(request, response);
}
else
{
System.out.println("有非法文字"); request.setAttribute("error", "有非法文字"); request.getRequestDispatcher("FilterWords.jsp").forward(reques