过滤器 Filter @WebFilter(URL)
- 方法
- void init(FilterConfig config) 初始化方法
- void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) 对请求或响应过滤
- void destroy() 销毁方法
- 多个filter,根据FilterMapping的顺序依次过滤
- 生命周期
- Server启动就init
- 过滤就doFilter
- 服务器关闭或应用被卸载就destroy
- FilterConfig 类似于ServletConfig
// 获取Filter的名字 public String getFilterName(); // 获取Servlet上下文 public ServletContext getServletContext(); // 根据初始化参数的名称获取初始化参数的值 public String getInitParameter(String paramName); // 获取所有初始化参数的名字 public Enumeration getInitParameterNames();
- FilterChain
// 放行 // 相当于执行Serlvet中的Service(ServletRequest req, ServletResponse res)方法 // 之后继续执行Filter类中的方法 public void doFilter(ServletRequest req, ServletResponse res)
- 过滤器五种拦截行为
- 过滤器默认拦截的是请求,但是在实际开发中,我们还有请求转发和请求包含,以及由服务器 出发调用的全局错误页面。默认情况下过滤器是不参数过滤的,要想使用,需要我们手动配置
- 在配置文件中,filter-mapping 里使用 进行配置
- REQUEST 默认值,过滤请求
- ERROR 过滤全局错误页面,当服务器调用全局错误页面时,过滤器工作
- FORWORD 过滤请求转发,当请求转发时,过滤器工作 转发到Filter配置的URLpattern时
- INCLUDE 过滤请求包含,当请求包含时,过滤器工作。同转发只能过滤动态包含,JSP的include指令是静态包含
- ASYNC 过滤异步类型,它要求我们在Filter标签配置中开启异步支持
监听器 @WebListener
- 在程序中,我们可以对:对象的创建销毁,域对象中属性的变化,会话相关的内容进行监听
- Servlet规范中共计8个监听器,都是以接口形式提供,具体功能需要我们自己完成
- 分类
- 监听对象创建和销毁
- ServletContextListener 用于监听ServletContext对象的创建和销毁
- void contextInitialized(ServletContextEvent sce) 对象创建时执行该方法
- void contextDestroyed(ServletContextEvent sce) 对象销毁时执行该方法
- ServletContextEvent sce 代表事件对象
- 事件对象中封装了事件源,也就是ServletContext
- HttpSessionListener 用于监听HttpSession对象的创建和销毁
- void sessionCreated(HttpSessionEvent se) 对象创建时执行该方法
- void sessionDestroyed(HttpSessionEvent se) 对象销毁时执行该方法
- HttpSessionEvent se 代表事件对象
- 事件对象中封装了事件源,也就是HttpSession
- ServletRequestListener 用于监听ServletRequest对象的创建和销毁
- void requestInitialized(ServletRequestEvent sre) 对象创建时执行该方法
- void requestDestroyed(ServletRequestEvent sre) 对象销毁时执行该方法
- ServletRequestEvent sre 代表事件对象
- 事件对象中封装了事件源,也就是ServletRequest
- ServletContextListener 用于监听ServletContext对象的创建和销毁
- 监听域对象属性变化
- ServletContextAttributeListener 用于监听ServletContext应用域中属性的变化
- void attributeAdded(ServletContextAttributeEvent scae) 域中添加属性时执行该方法
- void attributeRemoved(ServletContextAttributeEvent scae) 域中移除属性时执行该方法
- void attributeReplaced(ServletContextAttributeEvent scae) 域中替换属性时执行该方法
- HttpSessionAttributeListener 用于监听HttpSessio会话域中属性的变化
- void attributeAdded(HttpSessionBindingEvent se) 域中添加属性时执行该方法
- void attributeRemoved(HttpSessionBindingEvent se) 域中移除属性时执行该方法
- void attributeReplaced(HttpSessionBindingEvent se) 域中替换属性时执行该方法
- ServletRequestAttributeListener 用于监听ServletContext应用域中属性的变化
- void attributeAdded(ServletRequestAttributeEvent srae) 域中添加属性时执行该方法
- void attributeRemoved(ServletRequestAttributeEvent srae) 域中移除属性时执行该方法
- void attributeReplaced(ServletRequestAttributeEvent srae) 域中替换属性时执行该方法
- ServletContextAttributeListener 用于监听ServletContext应用域中属性的变化
- 监听会话相关的感知型监听器 感知型监听器,不需要配置
- HttpSessionBindingListener 用于感知对象和会话域绑定的监听器
- void valueBound(HttpSessionBindingEvent evnet) 添加数据到会话域中,执行该方法
- void valueUnbound(HttpSessionBindingEvent evnet) 数据从会话域中移除,执行该方法
- HttpSessionActivationListener 用于感知会话域中对象钝化和活化的监听器
- void sessionWillPassivate(HttpSessionEvent se) 会话域中数据钝化时执行该方法
- void sessionDidActivate(HttpSessionEvent se) 会话域中数据活化时执行该方法
- HttpSessionBindingListener 用于感知对象和会话域绑定的监听器
- 监听对象创建和销毁