(020)Spring Boot之Servlet、过滤器(Filter)、监听器(Listener)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了(020)Spring Boot之Servlet、过滤器(Filter)、监听器(Listener),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8640字,纯文字阅读大概需要13分钟。
内容图文
![(020)Spring Boot之Servlet、过滤器(Filter)、监听器(Listener)](/upload/InfoBanner/zyjiaocheng/1330/ce7d1d2ee1a148f7ad44431c984ceed1.jpg)
分别有两种方法实现Servlet、过滤器(Filter)、监听器(Listener)
先贴出pom.xml
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>springboot_web</artifactId> <version>1.0.0</version> <name>springboot_web</name> <!-- FIXME change it to the project‘s website --> <url>http://www.example.com</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
方法一,使用注解,(WebServlet、WebFilter、WebListener是Servlet3.0的注解)
(1)Servlet使用@ServletComponentScan+@WebServlet。举例如下:
UserServlet.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/user.do") publicclass UserServlet extends HttpServlet { privatestaticfinallong serialVersionUID = 1L; @Override protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("user servlet"); } }
App.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App. class , args); } }
浏览器输入:http://127.0.0.1:8080/user.do,验证结果如下:
(2)Filter使用@ServletComponentScan+@WebFilter。举例如下:
LogFilter.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; @WebFilter("/user.do") publicclass LogFilter implements Filter { @Override publicvoid destroy() { // TODO Auto-generated method stub } @Override publicvoid doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("income log filter "+arg0.getRemoteHost()); arg2.doFilter(arg0, arg1); } @Override publicvoid init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
App.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App. class , args); } }
浏览器输入:http://127.0.0.1:8080/user.do,验证结果如下:
(3)Listener使用@ServletComponentScan+@WebListener。举例如下:
MyContextListener.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import java.time.LocalDateTime; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class MyContextListener implements ServletContextListener { @Override publicvoid contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override publicvoid contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("start at "+LocalDateTime.now().toString()); } }
App.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App. class , args); } }
启动结果如下:
方法二,分别创建3个bean:ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean
servlet对应ServletRegistrationBean;Filter对应FilterRegistrationBean;Listenner对应ServletListenerRegistrationBean。请看代码:
UserServlet.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/user.do") publicclass UserServlet extends HttpServlet { privatestaticfinallong serialVersionUID = 1L; @Override protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("user servlet"); } }
LogFilter.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; @WebFilter("/user.do") publicclass LogFilter implements Filter { @Override publicvoid destroy() { // TODO Auto-generated method stub } @Override publicvoid doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("income log filter "+arg0.getRemoteHost()); arg2.doFilter(arg0, arg1); } @Override publicvoid init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
MyContextListener.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import java.time.LocalDateTime; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class MyContextListener implements ServletContextListener { @Override publicvoid contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override publicvoid contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("start at "+LocalDateTime.now().toString()); } }
ServletConfig.java。创建bean,并且装配到spring容器中
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import java.util.Arrays; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; @SpringBootConfiguration public class ServletConfig { @Bean public ServletRegistrationBean createBookServlet(){ ServletRegistrationBean servlet= new ServletRegistrationBean(new UserServlet(),"/user.do"); return servlet; } @Bean public FilterRegistrationBean createEchoFilter(){ FilterRegistrationBean filter=new FilterRegistrationBean(); filter.setFilter(new LogFilter()); filter.setUrlPatterns(Arrays.asList("/user.do")); return filter; } @Bean public ServletListenerRegistrationBean<MyContextListener> createStartedUpListener(){ ServletListenerRegistrationBean<MyContextListener> servletRegistrationBean=new ServletListenerRegistrationBean(new MyContextListener()); return servletRegistrationBean; } }
App.java
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323280.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/31/20221031052323441.jpg)
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App. class , args); } }
启动并验证结果如下:
浏览器输入:http://127.0.0.1:8080/user.do ,验证Servlet
验证Filter
原文:https://www.cnblogs.com/javasl/p/11918182.html
内容总结
以上是互联网集市为您收集整理的(020)Spring Boot之Servlet、过滤器(Filter)、监听器(Listener)全部内容,希望文章能够帮你解决(020)Spring Boot之Servlet、过滤器(Filter)、监听器(Listener)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。