一、Filter
1.过滤器介绍
-在程序中访问服务器资源时,当一个请求到来,服务器首先判断是否有过滤器与请求资源相关联,
如果有,过滤器可以将请求拦截下来,完成一些特定的功能,再由过滤器决定是否交给请求资源。
如果没有则像之前那样直接请求资源了。响应也是类似的!
-过滤器一般用于完成通用的操作,例如:登录验证、统一编码处理、敏感字符过滤等等~~~
2.过滤器核心方法
-Filter 是一个接口。如果想实现过滤器的功能,必须实现该接口!
-核心方法
返回值 方法名 作用
void init(FilterConfig config) 初始化方法
void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) 对请求资源和响应资源过滤
void destroy() 销毁方法
3.过滤器使用细节
配置方式
方式一:注解方式 @WebFilter(拦截路径)
方式二:配置文件方式(如果有多个过滤器,取决于过滤器映射的顺序)
<filter>
<filter-name></filter-name>
<filter-class></filter-class>
</filter>
<filter-mapping>
<filter-name></filter-name>
<url-pattern></url-parrern>
</filter-mapping>
4.过滤器生命周期
创建---当应用加载时实例化对象并执行 init 初始化方法。
服务---对象提供服务的过程,执行 doFilter 方法。
销毁---当应用卸载时或服务器停止时对象销毁。执行 destroy 方法。
5.FilterChain 介绍
-FilterChain 是一个接口,代表过滤器链对象。由 Servlet 容器提供实现类对象。直接使用即可。
-过滤器可以定义多个,就会组成过滤器链。
如果有多个过滤器,在第一个过滤器中调用下一个过滤器,依次类推。直到到达最终访问资源。
如果只有一个过滤器,放行时,就会直接到达最终访问资源。
-核心方法
返回值 方法名 作用
void doFilter(ServletRequest request,ServletResponse response) 放行方法
6.FilterConfig 介绍
-FilterConfig 是一个接口。代表过滤器的配置对象,可以加载一些初始化参数。
-核心方法
返回值 方法名 作用
String getFilterName() 获取过滤器对象名称
String getInitParameter(String key) 根据key获取value
Enumeration<String> getInitParameterNames() 获取所有参数的key
ServletContext getServletContext() 获取应用上下文对象
7.过滤器的五种拦截行为
-Filter 过滤器默认拦截的是请求,但是在实际开发中,我们还有请求转发和请求包含,以及由服务器触发调用的全局错误页面。默认情况下过滤器是不参与过滤的,要想使用,就需要我们配置。
<!--配置过滤器-->
<filter>
<filter-name>filterDemo05</filter-name>
<filter-class>com.itheima.filter.FilterDemo05</filter-class>
<!--配置开启异步支持,当dispatcher配置ASYNC时,需要配置此行-->
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>filterDemo05</filter-name>
<!--<url-pattern>/error.jsp</url-pattern>-->
<url-pattern>/index.jsp</url-pattern>
<!--过滤请求:默认值。-->
<dispatcher>REQUEST</dispatcher>
<!--过滤全局错误页面:当由服务器调用全局错误页面时,过滤器工作-->
<dispatcher>ERROR</dispatcher>
<!--过滤请求转发:当请求转发时,过滤器工作。-->
<dispatcher>FORWARD</dispatcher>
<!--过滤请求包含:当请求包含时,过滤器工作。它只能过滤动态包含,jsp的include指令是静态包含,过滤器不会起作用-->
<dispatcher>INCLUDE</dispatcher>
<!--过滤异步类型,它要求我们在filter标签中配置开启异步支持-->
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
另外补充一点错误页面配置
<!--配置全局错误页面-->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
二、Listerer
1.监听器介绍
-观察者设计模式,所有的监听器都是基于观察者设计模式的!
-三个组成部分
事件源:触发事件的对象
事件:触发的动作,封装了事件源
监听器:当事件源触发事件后,可以完成功能
-在程序当中,我们可以对:对象的创建销毁、域对象中属性的变化、会话相关内容进行监听。
-Servlet 规范中共计 8 个监听器,监听器都是以接口形式提供,具体功能需要我们自己来完成。
2.监听对象的监听器
①ServletContextListener:用于监听 ServletContext 对象的创建和销毁
核心方法
返回值 方法名 作用
void contextInitialized(ServletContextEvent sce) 对象创建时执行该方法
void contextDestroyed(ServletContextEvent sce) 对象销毁时执行该方法
参数:ServletContextEvent 代表事件对象
事件对象中封装了事件源,也就是 ServletContext
真正的事件指的是创建或销毁 ServletContext 对象的操作
②HttpSessionListener:用于监听 HttpSession 对象的创建和销毁
核心方法
返回值 方法名 作用
void sessionCreated(HttpSessionEvent se) 对象创建时执行该方法
void sessionDestroyed(HttpSessionEvent se) 对象销毁时执行该方法
参数:HttpSessionEvent 代表事件对象
事件对象中封装了事件源,也就是 HttpSession
真正的事件指的是创建或销毁 HttpSession 对象的操作
③ServletRequestListener:用于监听 ServletRequest 对象的创建和销毁
核心方法
返回值 方法名 作用
void requestInitialized(ServletRequestEvent sre) 对象创建时执行该方法
void requestDestroyed(ServletRequestEvent sre) 对象销毁时执行该方法
参数:ServletRequestEvent 代表事件对象
事件对象中封装了事件源,也就是 ServletRequest
真正的事件指的是创建或销毁 ServletRequest 对象的操作
3.监听域对象属性变化的监听器
④ServletContextAttributeListener:用于监听 ServletContext 应用域中属性的变化
核心方法
返回值 方法名 作用
void attributeAdded(ServletContextAttributeEvent scae) 域中添加属性时执行该方法
void attributeRemoved(ServletContextAttributeEvent scae) 域中移除属性时执行该方法
void attributeReplaced(ServletContextAttributeEvent scae) 域中替换属性时执行该方法
参数:ServletContextAttributeEvent 代表事件对象
事件对象中封装了事件源,也就是 ServletContext
真正的事件指的是添加、移除、替换应用域中属性的操作
⑤HttpSessionAttributeListener:用于监听 HttpSession 会话域中属性的变化
核心方法
返回值 方法名 作用
void attributeAdded(HttpSessionBindingEvent se) 域中添加属性时执行该方法
void attributeRemoved(HttpSessionBindingEvent se) 域中移除属性时执行该方法
void attributeReplaced(HttpSessionBindingEvent se) 域中替换属性时执行该方法
参数:HttpSessionBindingEvent 代表事件对象
事件对象中封装了事件源,也就是 HttpSession
真正的事件指的是添加、移除、替换会话域中属性的操作
⑥ServletRequestAttributeListener:用于监听 ServletRequest 请求域中属性的变化
核心方法
返回值 方法名 作用
void attributeAdded(ServletRequestAttributeEvent srae) 域中添加属性时执行该方法
void attributeRemoved(ServletRequestAttributeEvent srae) 域中移除属性时执行该方法
void attributeReplaced(ServletRequestAttributeEvent srae) 域中替换属性时执行该方法
参数:ServletRequestAttributeEvent 代表事件对象
事件对象中封装了事件源,也就是 ServletRequest
真正的事件指的是添加、移除、替换请求域中属性的操作
4.监听会话相关的感知型监听器
⑦HttpSessionBindingListener:用于感知对象和会话域绑定的监听器
核心方法
返回值 方法名 作用
void valueBound(HttpSessionBindingEvent event) 数据添加到会话域中(绑定)时执行该方法
void valueUnbound(HttpSessionBindingEvent event) 数据从会话域中移除(解绑)时执行该方法
参数:HttpSessionBindingEvent 代表事件对象
事件对象中封装了事件源,也就是 HttpSession
真正的事件指的是添加、移除会话域中数据的操作
⑧HttpSessionActivationListener:用于感知会话域中对象钝化和活化的监听器
核心方法
返回值 方法名 作用
void sessionWillPassivate(HttpSessionEvent se) 会话域中数据钝化时执行该方法
void sessionDidActivate(HttpSessionEvent se) 会话域中数据活化时执行该方法
参数:HttpSessionEvent 代表事件对象
事件对象中封装了事件源,也就是 HttpSession
真正的事件指的是会话域中数据钝化、活化的操作
5.监听器的使用
-监听对象的
--ServletContextListener
--HttpSessionListener
--ServletRequestListener
-监听属性变化的
--ServletContextAttributeListener
--HttpSessionAttributeListener
--ServletRequestAttributeListener
-会话相关的感知型
--HttpSessionBindingListener
--HttpSessionActivationListener