过滤器 Filter Filter也称之为过滤器,它是Servlet技术中比较激动人心的技术,
WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,
从而实现一些特殊的功能。
例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。
通过Filter技术,开发人员可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截。
Filters是在web.xml中配置的插件,Servlets和Filters相互没有依赖,如果通过编辑web.xml来添加和删除过滤器。
filter 优先级高于 servlet。
接口 Filter (实现即可 javax.servlet.Filter)
三个方法:
public void init(FilterConfig filterConfig)
throws ServletException
/*在filter被加载到service中的时候被container调用,
Servlet container实例化完filter以后立即调用Filter的init方法,
init方法中的工作必须在执行过滤过滤任务之前正确的完成。*/
/*在以下情况下web container不能把filter加载到service中:
1、抛出ServletException异常。
2、在container定义的时间内没有返回。*/
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
throws java.io.IOException,ServletException
/*Filter中的doFilter方法在每次request/response时候会被container调用,
能够传递到doFilter中的request或者response可以传递到Filter环中的下一个环节。
此时的Filter在设计模式中被称作责任链模式结构,这里面抽象处理者的角色就是javax.servlet.Filter这个接口,
注册的所有Filter是具体的处理者,在doFilter方法中实现具体处理逻辑,
在这里面责任链是一条直线,构成这条直线的就是所有注册的Filter。*/
public void destroy()
/*当filter从service中移除的时候,container调用destroy方法,通过调用这个方法,释放Filter所占有的系统资源。*/用法:
在web.xml中配置Filter:
EcodingFilter
EcodingFilter
org.nb.filter.EnCodeFilter
EncodeCoding
UTF-8
给Filter匹配指定的servlet 资源或者url-pattern:
EcodingFilter
*
*
在这里需要注意的是,在给servlet注册filter环,container首先处理的是url-patterns,
然后才处理servlet-names,所以如果要对filter的执行顺序有要求,那么在此需要注意。
可以借鉴的几个过滤器小例子:点击打开链接
监听器 Listener
能够监听到session,application的create,destroy,可以监听到session,application
属性绑定的变化,考虑了一下,可以应用在"在线人数统计","数据缓存"等各个方面
Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。
当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。
只要实现各个作用域所对应的Listener接口。
Interface ServletContextListener
两个方法:
1) public void contextInitialized(ServletContextEvent sce) 创建
2) public void contextDestroyed(ServletContextEvent sce) 销毁
Interface HttpSessionListener
两个方法:
1) public void sessionCreated(HttpSessionEvent se)
2) public void sessionDestroyed(HttpSessionEvent se)
Servlet中的Listener和Event:
1、在JSP 2.0/Servlet 2.4中,共有八个Listener接口,六个Event类别。
1)ServletContextListener接口
[接口方法] contextInitialized()与 contextDestroyed()
[接收事件] ServletContextEvent
[触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),
而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。
2)ServletContextAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] ServletContextAttributeEvent
[触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),
同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。
3)HttpSessionListener
[接口方法] sessionCreated()与sessionDestroyed ()
[接收事件] HttpSessionEvent
[触发场景] 在session (HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。
4)HttpSessionAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),
同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
5)HttpSessionActivationListener
[接口方法] sessionDidActivate()与 sessionWillPassivate()
[接收事件] HttpSessionEvent
[触发场景] Activate与Passivate是用于置换对象的动作,
当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透过对象序列化),
所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容易理解的,
sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。
6)ServletRequestListener
[接口方法] requestInitialized()与 requestDestroyed()
[接收事件] RequestEvent
[触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。
7)ServletRequestAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),
同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
8)HttpSessionBindingListener
[接口方法] valueBound()与valueUnbound()
[接收事件] HttpSessionBindingEvent
[触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入至session(HttpSession)对象的属性中,
则会呼叫 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会呼叫valueUnbound(),
实现HttpSessionBindingListener接口的类别不需在web.xml中设定。
2. 如何注册Servlet中的事件
实现上面这几个接口的类别,除了HttpSessionBindingListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时呼叫对应的类别,如:
< listener >
< listener-class > demo.servlet.listener.CustomServletContextListener
3.Java类实例
//侦听启动和关闭
import javax.servlet.ServletContextListener;
import javax.servlet.*;
public class TigerListen implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce)
{
System.out.print("Init") ;
}
public void contextDestroyed(ServletContextEvent sce)
{
System.out.print("Destroved") ;
}
}对应的web.xml是:
TigerListen