监听request对象的两个接口
ServletRequestListener
ServletRequestAttributeListener
接口的作用及方法
ServletRequestListener
监听request的创建和销毁
1 requestInitialized(ServletRequestEvent sre)
2.requestDestroyed(ServletRequestEvent sre)
ServletRequestAttributeListener
AttributeAdded(HttpSessionBindingEven event) 添加
AttributeRemoved(HttpSessionBindingEven event) 删除
AttributeReplaced(HttpSessionBindingEven event) 修改
在web.xml中配置监听
<listener>
<listener-class>包名 点 类名</listener-class>
</listener>
监听session对象的接口
HttpSessionListener
HttpSessionAttributeListener
接口的方法及作用
监听session的创建和销毁:HttpSessionListener
SessionCreated(HttpSessionEvent se);创建session对象
SessionDestroyed(HttpSessionEvent se) 销毁session对象
监听session作用域数据的变更:HttpSessionAttributeListener
AttributeAdded(HttpSessionBindingEvent event) 添加
AttributeRemoved(HttpSessionBindingEvent event) 移除
AttributeReplaced(HttpSessionBindingEvent event) 修改
监听application对象的接口
ServletContextListener
ServletContextAttributeListener
方法及作用
ServletContextListener:监听application对象的创建和销毁
contextInitialized(ServletContextEvent sce) application对象的创建
contextDestroyed(ServletContextEvent sce) 销毁
ServletContextAttributeListtener 监听application对象的变更
AttributeAdded(ServletContextAttributeEvent event)
AttributeRemoved(ServletContextAttributeEvent event)
AttributeReplaced(ServletContextAttributeEvent event)
过滤器
引入过滤器可以提高安全性和减少资源浪费
创建过滤器需要实现的三个方法
Init() doFilter() Destroy()
在web.xml中配置过滤器
<filter>
<filter-name>配置的过滤器名称</filter-name>
<filter-class>要配置的过滤器的全限定路径:包名.类名</filter-class>
</filter>
<filter-mapping>
<filter-name>配置的过滤器名称</filter-name>
<url-pattern>过滤器拦截请求地址的范围</url-pattern>
</filter-mapping>
过滤器之doFilter方法
作用:服务器在接收到浏览器发过来的请求后,先解析请求信息,创建对象request和
response对象,然后根据请求url地址判断如果符合过滤器的过滤范围,则会调用过滤器
中的doFilter来进行请求拦截,并将request和response对象作为实参传递给doFilter
方法。我们可以在doFilter方法中声明过滤器拦截代码。
参数:
ServletRequest:接收此次拦截的请求的request实参
ServletResponse :接收此次拦截请求的response实参
FilterChain :可以进行请求放行
chain.doFilter(request,response);
过滤器init和destory方法
init()方法 服务器加载的时候调用
destroy() 服务器关闭时调用
过滤器之拦截范围配置
拦截所有:/*
拦截部分Servlet的请求:*.do 拦截以 点 do 结尾的
拦截指定的Servlet的请求:和要拦截的指定的servlet的url-pattern配置完全一致即可
注意:过滤器之间会出现多重拦截,如果是按照拦截范围的大小为web.xml中自大而小进行的配置
则会先执行大范围的拦截器,在执行小范围的拦截器
网友评论