Servlet中的过滤器和监听器
过滤器,监听器,Servlet中的重要组件
- 过滤器:对从客户端向服务器端发送的请求进行过滤,也可以对服务器端返回的响应进行处理,简单来说就是对请求和响应做预处理操作

/** * @author: Mr.Wu * @description 创建一个过滤器 * @date: 2021/12/15 17:41 */ public class FirstFilter implements Filter { /** * 拦截请求和响应方法,用于对请求和响应实现预处理 * @param servletRequest * @param servletResponse * @param filterChain * @throws IOException * @throws ServletException */ @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("处理请求"); filterChain.doFilter(servletRequest, servletResponse); System.out.println("处理相应"); } /** * 初始化方法,在创建Filter之后立即调用,可用于完成初始化动作 * @param filterConfig * @throws ServletException */ @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init........."); } /** * 在销毁Filter之前自动调用,可以用于完成资源释放等动作 */ @Override public void destroy() { Filter.super.destroy(); } }
注意:filterChain.doFilter(servletRequest, servletResponse);之前是处理请求,之后是处理响应
使用Filter对请求编码进行设置:一般对请求编码进行拦截设置,对于相应编码在servlet配置,如果在Filter中对相应编码进行配置的时候不灵活
/** * @author: Mr.Wu * @description 使用Filter对请求编码进行设置,需要读取配置文件中init-param中的值 * @date: 2021/12/15 18:04 */ public class EncodingFilter implements Filter { private String coding; @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (coding==null){ servletRequest.setCharacterEncoding("utf-8"); }else { servletRequest.setCharacterEncoding(coding); } filterChain.doFilter(servletRequest, servletResponse); } @Override public void init(FilterConfig filterConfig) throws ServletException { //获取到<init-param>中定义的值 String code = filterConfig.getInitParameter("code"); this.coding=code; } @Override public void destroy() { Filter.super.destroy(); } }
Filter过滤器链:如果有多个过滤器对某个请求及响应进行过滤,这组过滤器就称为过滤器链,对于多个Filter执行顺序,是按照web.xml文件中配置的上下顺序来决定先后,在上的先执行,在下的后执行。和匹配规则以及精确度没有关系

Filter的生命周期:与自启动Servlet生命周期类似,Filter的生命周期是由容器来管理的,当容器启动时会实例化Filter并调用init()方法完成初始化动作。当客户端浏览器发送请求时,容器会启动一个新的线程来处理请求,如果请求的url能被过滤器所匹配,那么优先调用过滤器中的doFilter方法,再根据是否有chain.doFilter指令,决定是否请求目标资源,当容器关闭时会销毁Filter对象,在销毁之前会调用destory方法
基于注解式开发Filter
注意:使用注解式开发Filter时,执行顺序会根据Filter的名称 进行排序
/** * @author: Mr.Wu * @description 基于注解开发filter * @date: 2021/12/15 20:03 */ @WebFilter(urlPatterns = "/*",description = "注解式开发filter测试",initParams = {@WebInitParam(name = "name",value = "curry"),@WebInitParam(name = "name1",value = "curry1")}) public class AnnotationFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println(filterConfig.getInitParameter("name")); System.out.println(filterConfig.getInitParameter("name1")); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("annotationFilter....."); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { Filter.super.destroy(); } }
- 监听器Listener:监听器用于监听web应用中某些对象的创建、销毁、增加、修改、删除等动作的发生,然后做出响应的响应处理,当范围对象的状态发生变化的时候,服务器会自动调用监听器对象中的方法。监听器的用处:做用户访问人数的统计、spring框架启动
监听器的分类(按照监听的对象划分):
1、ServletContext对象生命周期监听器与属性操作监听器,写好servletcontextListener之后同样需要在web.xml中进行配置
/** * @author: Mr.Wu * @description ServletContext生命周期监听器 * @date: 2021/12/15 20:27 */ public class ServletContextLifeListener implements ServletContextListener { /** * 在容器创建了servletContext对象之后会立即调用scecontextInitialized方法 * @param */ @Override public void contextInitialized(ServletContextEvent sce) { } /** * 在ServletContext对象销毁之前会调用contextDestroyed方法 * @param sce */ @Override public void contextDestroyed(ServletContextEvent sce) { ServletContextListener.super.contextDestroyed(sce); } }
/** * @author: Mr.Wu * @description ServletContext对象属性操作监听器 * @date: 2021/12/15 20:40 */ public class ServletContextAttrListener implements ServletContextAttributeListener { /** * 当servletContext对象进行添加值操作的时候会执行该方法,会把添加的name,value,servletContext对象都传递到scae中 * @param scae */ @Override public void attributeAdded(ServletContextAttributeEvent scae) { scae.getName(); scae.getValue(); scae.getServletContext(); } /** * 当servletContext对象进行移除值操作的时候会执行该方法,会把添加的name,value,servletContext对象都传递到scae中 * @param scae */ @Override public void attributeRemoved(ServletContextAttributeEvent scae) { scae.getName(); scae.getValue(); scae.getServletContext(); } /** * 当servletContext对象进行替换值操作的时候会执行该方法,会把添加的name,value,servletContext对象都传递到scae中 * @param scae */ @Override public void attributeReplaced(ServletContextAttributeEvent scae) { scae.getName(); scae.getValue(); scae.getServletContext(); } }
2、HttpSession对象生命周期监听器与属性操作监听器
/** * @author: Mr.Wu * @description HttpSession对象生命周期监听器 * @date: 2021/12/15 21:01 */ public class HttpSessionLiftCycleListener implements HttpSessionListener { /** * 当创建session的时候会调用这个方法,并把session对象传入这个方法中 * @param se */ @Override public void sessionCreated(HttpSessionEvent se) { HttpSession session = se.getSession(); System.out.println(session); System.out.println("sessionCreated"); } /** * 当httpSession对象销毁之前会调用这个监听器 * 值得注意的是:销毁HTTP Session对象只有两种方式 *为:调用invalidate()方法,或者是session的超时时间结束,关闭浏览器并不能销毁session,只是浏览器对于session对象没了 * @param se */ @Override public void sessionDestroyed(HttpSessionEvent se) { HttpSession session = se.getSession(); System.out.println(session); System.out.println("sessionDestroy"); } }
3、ServletRequest对象生命周期监听器与属性操作监听器-----与ServletContext对象生命周期监听器与属性操作监听器大概类似
本文来自博客园,作者:(HelloWorld!),转载请注明原文链接:https://www.cnblogs.com/Simon-s/articles/15694497.html --有志者,事竟成

浙公网安备 33010602011771号