Intercepting Filter设计模式,在Web曾处理前进行过滤。一种很有用的过滤是用户是否登陆的安全性检查。即在以下的doFilter函数中添加安全性检查的语句,这样就不需要对每个页面(servlet)进行安全性配置。
通常通过实现javax.servlet.Filter接口实现,其主要方法有:
Method Summary | |
void | destroy() Called by the web container to indicate to a filter that it is being taken out of service. |
void | doFilter(ServletRequest request, ServletResponse response, FilterChain chain) The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. |
void | init(FilterConfig filterConfig) Called by the web container to indicate to a filter that it is being placed into service. |
然后在web.xml中配置过滤器即可。