您的位置:首页 > 运维架构 > Apache

Apache Shiro学习笔记(六)Servlet3.0 Filter介绍

2016-08-02 09:50 573 查看
鲁春利的工作笔记,好记性不如烂笔头

Filter接口
  Filter也称之为过滤器,通过Filter对所有web资源(例如Jsp, Servlet, 静态文件等)进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等。
package javax.servlet;

/**
* A filter is an object that performs filtering tasks on either the
* request to a resource (a servlet or static content), or on the response
* from a resource, or both.
*
* @since Servlet 2.3
*/
public interface Filter {

public void init(FilterConfig filterConfig) throws ServletException;

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException;

public void destroy();
}
  Filter的创建和销毁由WEB服务器负责。 web 应用程序启动时,web 服务器将创建Filter的实例对象,并调用其init方法,完成对象的初始化功能,从而为后续的用户请求作好拦截的准备工作,filter对象只会创建一次,init方法也只会执行一次。通过init方法的参数,可获得代表当前filter配置信息的FilterConfig对象。

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上下文对象的引用。

Decorator设计模式

本文出自 “闷葫芦的世界” 博客,请务必保留此出处http://luchunli.blog.51cto.com/2368057/1833363
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: