[2].实例:报告过滤器
趁热打铁,我们来试验一个简单的过滤器,只要调用相关的servlet或JSP页面,它就打印一条消息到标准输出。为了完成此任务,相应的过滤器必须具有下面的内容:
1)实现Filter接口的一个类。这个类名为ReportFilter,如程序清单9-2所示。这个类对init和destroy方法提供空体。
2)在doFilter方法中过滤行为。每当调用与这个过滤器相关的servlet或JSP页面时,doFilter方法就生成一个打印输出,此输出列出请求主机和调用的URL。因为getRequestURL方法位于HttpServletRequest而不是ServletRequest中,所以把ServletRequest对象构造为HttpServletRequest类型。
3)调用FilterChain的doFilter方法。在打印输出报告后,过滤器调用FilterChain的doFilter方法激活servlet或JSP页面(如果有的话,调用下一个过滤器)
4)对Web应用主页和显示TodaysSpecialservlet进行注册。首先,filter元素将名称Reporter与类moreservlets.filters.ReportFilter相关联。然后,filter-mapping元素使用/index.jsp的url-pattern将过滤器与主页相关联。最后,filter-mapping元素使用TodaysSpecial的servlet-name将过滤器与TodaysSpecialservlet(名称TodaysSpecial是在servlet元素中声明的)相关联。参见程序清单9-3。
5)禁用激活器servlet。首先,建立一个RedirectorServlet(见程序清单9-6),它把接收到的所有请求重定向到此Web应用的主页。接着,利用servlet和servlet-mapping元素(参见程序清单9-3)指定所有以http://host/webAppPrefix/servlet/ 开始的URL都应该激活RedirectorServlet。
给出这些设置后,每当客户机请求此Web应用主页(程序清单9-4)或TodaysSpecialservlet(程序清单9-5)时,都调用此过滤器。
程序清单9-2 ReportFilter.java
package moreservlets.filters;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*; // For Date class
public class ReportFilter implements Filter {
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest)request;
System.out.println(req.getRemoteHost() +
" tried to access " +
req.getRequestURL() +
" on " + new Date() + ".");
chain.doFilter(request,response);
}
public void init(FilterConfig config)
throws ServletException {
}
public void destroy() {}
}
程序清单9-3 web.xml(针对报告过滤器的摘录)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- Register the name "Reporter" for ReportFilter. -->
<filter>
<filter-name>Reporter</filter-name>
<filter-class>
moreservlets.filters.ReportFilter
</filter-class>
</filter>
<!-- -->
<!-- Apply the Reporter filter to home page. -->
<filter-mapping>
<filter-name>Reporter</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<!-- Also apply the Reporter filter to the servlet named
"TodaysSpecial".
-->
<filter-mapping>
<filter-name>Reporter</filter-name>
<servlet-name>TodaysSpecial</servlet-name>
</filter-mapping>
<!-- -->
<!-- Give a name to the Today's Special servlet so that filters
can be applied to it.
-->
<servlet>
<servlet-name>TodaysSpecial</servlet-name>
<servlet-class>
moreservlets.TodaysSpecialServlet
</servlet-class>
</servlet>
<!-- -->
<!-- Make /TodaysSpecial invoke the servlet
named TodaysSpecial (i.e., moreservlets.TodaysSpecial).
-->
<servlet-mapping>
<servlet-name>TodaysSpecial</servlet-name>
<url-pattern>/TodaysSpecial</url-pattern>
</servlet-mapping>
<!-- Turn off invoker. Send requests to index.jsp. -->
<servlet-mapping>
<servlet-name>Redirector</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
<!-- -->
</web-app>
转:http://hi.baidu.com/javatwt/blog/item/d0b5bcef3c164fefcf1b3e11.html