本例介绍Servlet三大组件 Servlet、Filter、Listener
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。 注册三大组件用以下方式
Servlet
1、新建一个SpringBoot Web项目,新建一个MyServlet类,继承 HttpServlet
1 package com.test.springboot.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 9 public class MyServlet extends HttpServlet { 10 11 @Override 12 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 13 doPost(req, resp); 14 } 15 16 @Override 17 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 18 System.out.println("=====MyServlet====="); 19 super.doPost(req, resp); 20 } 21 }
2、注入Servlet,SpringBoot注入Servlet有2种方式
方法一:使用注入ServletRegistrationBean方式注入Servlet
1 @Bean 2 public ServletRegistrationBean myServlet(){ 3 ServletRegistrationBean registrationBean = new ServletRegistrationBean( ); 4 registrationBean.setServlet(new MyServlet()); 5 registrationBean.setUrlMappings(Arrays.asList("/myServlet")); 6 return registrationBean; 7 }
方法二:使用@WebServlet注解的方式注入
在MyServlet类上使用@WebServlet,并在SpringBoot启动类上使用@ServletComponentScan注解,扫描被注解的Servlet
1 @WebServlet(urlPatterns = "/myServlet") 2 public class MyServlet extends HttpServlet {
Application.java
1 @SpringBootApplication 2 // 默认扫描当前包及子目录下的类 3 @ServletComponentScan 4 public class Application { 5 6 public static void main(String[] args) { 7 // Spring应用启动 8 SpringApplication.run(Application.class, args); 9 } 10 }
3、测试,在浏览器中输入地址:ip:端口/项目名/myServlet,即可访问MyServlet
Filter
1、新建一个SpringBoot Web项目,新建一个MyFilter类,实现Filter接口
1 package com.test.springboot.filter; 2 3 import javax.servlet.*; 4 import javax.servlet.http.HttpFilter; 5 import java.io.IOException; 6 7 public class MyFilter implements Filter { 8 9 @Override 10 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 11 System.out.println("====MyFilter===="); 12 chain.doFilter(request, response); 13 } 14 }
2、注入Filter,SpringBoot注入Filter有2种方式
方法一:使用注入FilterRegistrationBean方式注入Filter
1 @Bean 2 public FilterRegistrationBean myFilter(){ 3 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter()); 4 filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); 5 return filterRegistrationBean; 6 }
方法二:使用@WebFilter注解的方式注入
在MyFilter类上使用@WebFilter,并在SpringBoot启动类上使用@ServletComponentScan注解,扫描被注解的Filter,同上
1 @WebFilter(filterName = "myFilter",urlPatterns = "/*") 2 public class MyFilter implements Filter {
3、测试,请求任意地址都会经过MyFilter
Listener
1、新建一个SpringBoot Web项目,新建一个MyListener类
1 package com.test.springboot.listener; 2 3 4 import javax.servlet.ServletContextEvent; 5 import javax.servlet.ServletContextListener; 6 7 public class MyListener implements ServletContextListener { 8 9 public void contextInitialized(ServletContextEvent sce) { 10 System.out.println("contextInitialized....."); 11 } 12 13 public void contextDestroyed(ServletContextEvent sce) { 14 System.out.println("contextDestroyed....."); 15 } 16 }
2、注入Filter,SpringBoot注入Listener有2种方式
方法一:使用注入ServletListenerRegistrationBean方式注入Listener
1 @Bean 2 public ServletListenerRegistrationBean myListener(){ 3 ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean(new MyListener()); 4 return listener; 5 }
方法二:使用@WebListener注解的方式注入
在MyListener类上使用@WebListener,并在SpringBoot启动类上使用@ServletComponentScan注解,扫描被注解的Listener,同上
1 @WebListener 2 public class MyListener implements ServletContextListener {
3、测试,项目启动即可看到Listener运行