实现步骤:
1、编写实现了Filter接口的类,其中doFilter()方法表示filter的具体功能实现,init()表示filter初始化时要执行的操作,destrory()表示filter销毁时要执行的操作。特别的,如果同意用户对目标资源的访问,就调用FilterChain的doFilter()方法。
2、在web.xml中的注册filter,并配置filter的作用对象或范围。
作用:
1、在访问目标资源前执行一些操作
2、决定是否访问目标资源
3、访问目标资源之后执行一些操作
FilterConfig接口:
用户在配置filter时,可以使用<init-param>为filter配置一些初始化参数,当web容器实例化Filter对象,调用其init方法时,会把封装了filter初始化参数的filterConfig对象传递进来。因此开发人员在编写filter时,通过filterConfig对象的方法,就可获得:
String getFilterName():得到filter的名称。
String getInitParameter(String name): 返回在部署描述中指定名称的初始化参数的值。如果不存在返回null.
Enumeration getInitParameterNames():返回过滤器的所有初始化参数的名字的枚举集合。
public ServletContext getServletContext():返回Servlet上下文对象的引用。
Filter配置:
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>com.cserver.saas.system.commons.filter.SingleSignOutFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
本文参考:http://www.cnblogs.com/xdp-gacl/p/3948353.html