Java的Servlet、Filter、Interceptor、Listener

写在前面:

使用Spring-Boot时,嵌入式Servlet容器可以通过扫描注解(@ServletComponentScan)的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。 

Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。一般情况系统默认的Servlet就够用了,如果需要自定义Servlet,可以继承系统抽象类HttpServlet,重写方法来实现自己的Servlet。关于Servlet、过滤器、拦截器、监听器可以参考:(转)servlet、filter、listener、interceptor之间的区别和联系

Spring-Boot有两种方法注册Servlet、Filter和Listener :

1、代码注册:通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。

2、在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。

一、Servlet

Servlet匹配规则:匹配的优先级是从精确到模糊,复合条件的Servlet并不会都执行。

1、通过@ServletComponentScan自动扫描

a、springboot的启动入口添加注解:@ServletComponentScan;

 

@SpringBootApplication
@ServletComponentScan
public class ApplicationMain {public static void main(String[] args) {SpringApplication.run(ApplicationMain.class, args);}
}

 

b、@WebServlet 自定义Servlet,配置处理请求路径 /demo/myServlet 

@WebServlet(name = "myServletDemo1",urlPatterns = "/demo/myServlet",description = "自定义的servlet")
public class MyServletDemo1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("==========myServletDemo Get Method==========");resp.getWriter().println("my myServletDemo1 process request");super.doGet(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("==========myServletDemo1 POST Method==========");super.doPost(req, resp);}
}

 

2、使用@ServletRegistrationBean注解

a、@ServletRegistrationBean注入自定义的Servlet,配置处理的路径为 /demo/servletDemo2 

@Configuration
public class ServletConfiguration {/*** 代码注入*/@Beanpublic ServletRegistrationBean myServletDemo() {return new ServletRegistrationBean(new MyServletDemo2(), "/demo/servletDemo2");}
}

b、自定义的Servlet

public class MyServletDemo2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("==========myServletDemo2 Get Method==========");resp.getWriter().println("my myServletDemo2 process request");super.doGet(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("==========myServletDemo2 POST Method==========");super.doPost(req, resp);}
}

 

二、Filter

完整的流程是:Filter对用户请求进行预处理,接着将请求交给Servlet进行处理并生成响应,最后Filter再对服务器响应进行后处理。

Filter有如下几个用处。

  • 在HttpServletRequest到达Servlet之前,拦截客户的HttpServletRequest。
  • 根据需要检查HttpServletRequest,也可以修改HttpServletRequest头和数据。
  • 在HttpServletResponse到达客户端之前,拦截HttpServletResponse。
  • 根据需要检查HttpServletResponse,也可以修改HttpServletResponse头和数据。

多个FIlter可以组成过滤器调用链,按设置的顺序逐一进行处理,形成Filter调用链。

1、通过@ServletComponentScan自动扫描

a、springboot的启动入口添加注解:@ServletComponentScan;

b、@WebFilter 配置处理全部url的Filter

@WebFilter(filterName = "myFilter",urlPatterns = "/*")
public class MyFilter implements Filter {public void init(FilterConfig filterConfig) throws ServletException {System.out.println(">>>>>>myFilter init ……");}public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println(">>>>>>执行过滤操作");filterChain.doFilter(servletRequest, servletResponse);}public void destroy() {System.out.println(">>>>>>myFilter destroy ……");}
}

 * doFilter()方法是过滤器的核心方法,实现该方法就可实现对用户请求进行预处理,也可实现对服务器响应进行后处理——它们的分界线为是否调用了filterChain.doFilter(),执行该方法之前,即对用户请求进行预处理;执行该方法之后,即对服务器响应进行后处理。

2、通过@FilterRegistrationBean注册

a、@Bean注入自定义的Filter

@Configuration
public class ServletConfiguration {@Beanpublic FilterRegistrationBean myFilterDemo(){FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new MyFilter2());registration.addUrlPatterns("/demo/myFilter2");registration.addInitParameter("paramName", "paramValue");registration.setName("myFilter2");registration.setOrder(2);//指定filter的顺序return registration;}
}

b、自定义的Filter

public class MyFilter2 implements Filter {public void init(FilterConfig filterConfig) throws ServletException {System.out.println("======MyFilter2 init ……");}public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("======MyFilter2执行过滤操作");filterChain.doFilter(servletRequest, servletResponse);}public void destroy() {System.out.println("======MyFilter2 destroy ……");}
}

三、Listener

目前 Servlet 中提供了 6 种两类事件的观察者接口,它们分别是:4 个 EventListeners 类型的,ServletContextAttributeListener、ServletRequestAttributeListener、ServletRequestListener、HttpSessionAttributeListener 和 2 个 LifecycleListeners 类型的,ServletContextListener、HttpSessionListener。

  • ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。
  • ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
  • HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
  • HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

1、通过@ServletComponentScan自动扫描

a、ServletListenerRegistrationBean 注入自定义的Listener;

b、自定义的Listener

@WebListener
public class MyLisener implements ServletContextListener {public void contextInitialized(ServletContextEvent servletContextEvent) {System.out.println("MyLisener contextInitialized method");}public void contextDestroyed(ServletContextEvent servletContextEvent) {System.out.println("MyLisener contextDestroyed method");}
}

2、通过@ServletListenerRegistrationBean 注册

a、@Bean注入自定义的Listener;

@Configuration
public class ServletConfiguration {@Beanpublic ServletListenerRegistrationBean myListener(){return new ServletListenerRegistrationBean(new MyLisener());}
}

b、自定义的Listener

public class MyLisener implements ServletContextListener {public void contextInitialized(ServletContextEvent servletContextEvent) {System.out.println("MyLisener contextInitialized method");}public void contextDestroyed(ServletContextEvent servletContextEvent) {System.out.println("MyLisener contextDestroyed method");}
} 

四、验证servlet、filter、listener的顺序

a、使用MyServletDemo2、MyFilter2、MyFilter3、MyLisener做测试;

b、设置servlet处理url格式为 /demo/*;设置MyFilter2顺序为2;MyFilter3的顺序为3;

@Configuration
public class ServletConfiguration {/**代码注入*/@Beanpublic ServletRegistrationBean myServletDemo(){return new ServletRegistrationBean(new MyServletDemo2(),"/demo/*");}@Beanpublic FilterRegistrationBean myFilterDemo(){FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new MyFilter2());registration.addUrlPatterns("/demo/myFilter");registration.addInitParameter("paramName", "paramValue");registration.setName("myFilter2");registration.setOrder(2);//指定filter的顺序return registration;}@Beanpublic FilterRegistrationBean myFilterDemo2(){FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new MyFilter3());registration.addUrlPatterns("/demo/*");registration.addInitParameter("paramName", "paramValue");registration.setName("myFilter3");registration.setOrder(1);return registration;}@Beanpublic ServletListenerRegistrationBean myListener(){return new ServletListenerRegistrationBean(new MyLisener());}
}
View Code

c、启动项目后输出:(FIlter2先执行init,因为@Ben在前)

MyLisener contextInitialized method
======MyFilter2 init ……
======MyFilter3 init ……

d、浏览器输入地址地址:http://localhost:8080/demo/myFilter,输出:

======MyFilter3执行过滤操作
======MyFilter2执行过滤操作
>>>>>>>>>>test Get Method==========

可以看出:

  1. Filter3比Filter2先执行;
  2. Filter可以匹配上的url都会执行,并且按顺序执行(Filter的调用链);
  3. Filter比servlet先执行。
  4. servlet先按具体匹配,然后模糊匹配,并且只能有一个servlet匹配上,没有servlet调用链。

执行顺序是:Listener》Filter》Servlet

五、ApplicationListener自定义侦听器类

参考:http://blog.csdn.net/liaokailin/article/details/48186331

六、Interceptor

拦截器只会处理DispatcherServlet处理的url

a、自定义拦截器

public class MyInterceptor implements HandlerInterceptor {public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {System.out.println(">>>>MyInterceptor preHandle");return true;}public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println(">>>>MyInterceptor postHandle");}public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println(">>>>MyInterceptor afterCompletion");}
}

 

b、注册拦截器

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");super.addInterceptors(registry);}
}

c、拦截器验证

输入地址:http://localhost:8080/home/test

>>>>MyInterceptor preHandle
>>>>MyInterceptor postHandle
>>>>MyInterceptor afterCompletion

输入地址:http://localhost:8080/demo/myFilter (自定义的Servlet处理了请求,此时拦截器不处理)

拦截器不处理。

转载于:https://www.cnblogs.com/mr-yang-localhost/p/7784607.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390854.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html5教程_最好HTML和HTML5教程

html5教程HyperText Markup Language (HTML) is a markup language used to construct online documents and is the foundation of most websites today. A markup language like HTML allows us to超文本标记语言(HTML)是用于构造在线文档的标记语言,并且是当今大…

leetcode 1035. 不相交的线(dp)

在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数。 现在,可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线,这些直线需要同时满足满足: nums1[i] nums2[j] 且绘制的直线不与任何其他连线(非水平线&#x…

SPI和RAM IP核

学习目的: (1) 熟悉SPI接口和它的读写时序; (2) 复习Verilog仿真语句中的$readmemb命令和$display命令; (3) 掌握SPI接口写时序操作的硬件语言描述流程(本例仅…

个人技术博客Alpha----Android Studio UI学习

项目联系 这次的项目我在前端组,负责UI,下面简略讲下学到的内容和使用AS过程中遇到的一些问题及其解决方法。 常见UI控件的使用 1.TextView 在TextView中,首先用android:id给当前控件定义一个唯一标识符。在活动中通过这个标识符对控件进行事…

数据科学家数据分析师_站出来! 分析人员,数据科学家和其他所有人的领导和沟通技巧...

数据科学家数据分析师这一切如何发生? (How did this All Happen?) As I reflect on my life over the past few years, even though I worked my butt off to get into Data Science as a Product Analyst, I sometimes still find myself begging the question, …

leetcode 810. 黑板异或游戏

黑板上写着一个非负整数数组 nums[i] 。Alice 和 Bob 轮流从黑板上擦掉一个数字,Alice 先手。如果擦除一个数字后,剩余的所有数字按位异或运算得出的结果等于 0 的话,当前玩家游戏失败。 (另外,如果只剩一个数字,按位异…

react-hooks_在5分钟内学习React Hooks-初学者教程

react-hooksSometimes 5 minutes is all youve got. So in this article, were just going to touch on two of the most used hooks in React: useState and useEffect. 有时只有5分钟。 因此,在本文中,我们仅涉及React中两个最常用的钩子: …

分析工作试用期收获_免费使用零编码技能探索数据分析

分析工作试用期收获Have you been hearing the new industry buzzword — Data Analytics(it was AI-ML earlier) a lot lately? Does it sound complicated and yet simple enough? Understand the logic behind models but dont know how to code? Apprehensive of spendi…

select的一些问题。

这个要怎么统计类别数呢? 哇哇哇 解决了。 之前怎么没想到呢?感谢一楼。转载于:https://www.cnblogs.com/AbsolutelyPerfect/p/7818701.html

html5语义化标记元素_语义HTML5元素介绍

html5语义化标记元素Semantic HTML elements are those that clearly describe their meaning in a human- and machine-readable way. 语义HTML元素是以人类和机器可读的方式清楚地描述其含义的元素。 Elements such as <header>, <footer> and <article> …

重学TCP协议(12)SO_REUSEADDR、SO_REUSEPORT、SO_LINGER

1. SO_REUSEADDR 假如服务端出现故障&#xff0c;主动断开连接以后&#xff0c;需要等 2 个 MSL 以后才最终释放这个连接&#xff0c;而服务重启以后要绑定同一个端口&#xff0c;默认情况下&#xff0c;操作系统的实现都会阻止新的监听套接字绑定到这个端口上。启用 SO_REUSE…

残疾科学家_数据科学与残疾:通过创新加强护理

残疾科学家Could the time it takes for you to water your houseplants say something about your health? Or might the amount you’re moving around your neighborhood reflect your mental health status?您给植物浇水所需的时间能否说明您的健康状况&#xff1f; 还是…

POJ 3660 Cow Contest [Floyd]

POJ - 3660 Cow Contest http://poj.org/problem?id3660 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is un…

Linux 网络相关命令

1. telnet 1.1 检查端口是否打开 执行 telnet www.baidu.com 80&#xff0c;粘贴下面的文本&#xff08;注意总共有四行&#xff0c;最后两行为两个空行&#xff09; telnet [domainname or ip] [port]例如&#xff1a; telnet www.baidu.com 80 如果这个网络连接可达&…

JSON.parseObject(String str)与JSONObject.parseObject(String str)的区别

一、首先来说说fastjson fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器&#xff0c;来自阿里巴巴的工程师开发。其主要特点是&#xff1a; ① 快速&#xff1a;fastjson采用独创的算法&#xff0c;将parse的速度提升到极致&#xff0c;超过所有基于Java的jso…

jQuery Ajax POST方法

Sends an asynchronous http POST request to load data from the server. Its general form is:发送异步http POST请求以从服务器加载数据。 其一般形式为&#xff1a; jQuery.post( url [, data ] [, success ] [, dataType ] )url : is the only mandatory parameter. This…

spss23出现数据消失_改善23亿人口健康数据的可视化

spss23出现数据消失District Health Information Software, or DHIS2, is one of the most important sources of health data in low- and middle-income countries (LMICs). Used by 72 different LMIC governments, DHIS2 is a web-based open-source platform that is used…

01-hibernate注解:类级别注解,@Entity,@Table,@Embeddable

Entity Entity:映射实体类 Entity(name"tableName") name:可选&#xff0c;对应数据库中一个表&#xff0c;若表名与实体类名相同&#xff0c;则可以省略。 注意&#xff1a;使用Entity时候必须指定实体类的主键属性。 第一步&#xff1a;建立实体类&#xff1a; 分别…

leetcode 1707. 与数组中元素的最大异或值

题目 给你一个由非负整数组成的数组 nums 。另有一个查询数组 queries &#xff0c;其中 queries[i] [xi, mi] 。 第 i 个查询的答案是 xi 和任何 nums 数组中不超过 mi 的元素按位异或&#xff08;XOR&#xff09;得到的最大值。换句话说&#xff0c;答案是 max(nums[j] XO…

MySQL基础入门学习【2】数据类型

数据类型&#xff1a;指列、存储过程参数、表达式和局部变量的数据特征&#xff0c;它决定了数据的存储格式&#xff0c;代表了不同的信息类型 &#xff08;1&#xff09; 整型(按存储范围分类)&#xff1a;TINYINT&#xff08;1字节&#xff09; SAMLLINT&#xff08;2字节&am…