码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot Servlet 过滤 监听

时间:2017-08-30 13:19:03      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:creat   自动注册   extends   under   tty   serial   扫描   inter   vax   

Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。

当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。 
Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?

在spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(Filter和Listener也是如此)。 
一、代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。 
也可以通过实现 ServletContextInitializer 接口直接注册。

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

通过代码注册Servlet示例代码:

SpringBootSampleApplication.Java

 1 package org.springboot.sample;
 2 
 3 import org.springboot.sample.servlet.MyServlet;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.boot.context.embedded.ServletRegistrationBean;
 7 import org.springframework.boot.web.servlet.ServletComponentScan;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.web.servlet.DispatcherServlet;
10 
11 @SpringBootApplication
12 public class SpringBootSampleApplication {
13 
14     /**
15      * 使用代码注册Servlet(不需要@ServletComponentScan注解)
16      *
17      * @return
18      * @author SHANHY
19      * @create  2016年1月6日
20      */
21     @Bean
22     public ServletRegistrationBean servletRegistrationBean() {
23         return new ServletRegistrationBean(new MyServlet(), "/xs/*");// ServletName默认值为首字母小写,即myServlet
24     }
25     
26      /**
27      * 使用代码注册Servlet(不需要@ServletComponentScan注解)
28      *
29      * @return
30      * @author SHANHY
31      * @create  2016年1月6日
32      */
33    /* @Bean
34     public FilterRegistrationBean  filterRegistrationBean () {
35         FilterRegistrationBean registrationBean= new FilterRegistrationBean(new GoodsFilter());// ServletName默认值为首字母小写,即goodsFilter
36         registrationBean.addUrlPatterns("/goods/*");
37         return registrationBean;
38     }*/
39 
40 
41     public static void main(String[] args) {
42         SpringApplication.run(SpringBootSampleApplication.class, args);
43     }
44 }

 

 

MyServlet.java

 1 package org.springboot.sample.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 /**
13  * Servlet
14  *
15  * @author   单红宇(365384722)
16  * @myblog  http://blog.csdn.net/catoop/
17  * @create    2016年1月6日
18  */
19 //@WebServlet(urlPatterns="/xs/*", description="Servlet的说明")
20 public class MyServlet extends HttpServlet{
21 
22     private static final long serialVersionUID = -8685285401859800066L;
23 
24     @Override
25     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26         System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");
27         doPost(req, resp);
28     }
29 
30     @Override
31     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
32         System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");
33         resp.setContentType("text/html");  
34         PrintWriter out = resp.getWriter();  
35         out.println("<html>");  
36         out.println("<head>");  
37         out.println("<title>Hello World</title>");  
38         out.println("</head>");  
39         out.println("<body>");  
40         out.println("<h1>大家好,我的名字叫Servlet</h1>");  
41         out.println("</body>");  
42         out.println("</html>"); 
43     }
44 
45 }

使用注解注册Servlet示例代码

SpringBootSampleApplication.java

 1 package org.springboot.sample;
 2 
 3 import org.springboot.sample.servlet.MyServlet;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.boot.context.embedded.ServletRegistrationBean;
 7 import org.springframework.boot.web.servlet.ServletComponentScan;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.web.servlet.DispatcherServlet;
10 
11 @SpringBootApplication
12 @ServletComponentScan
13 public class SpringBootSampleApplication {
14 
15     public static void main(String[] args) {
16         SpringApplication.run(SpringBootSampleApplication.class, args);
17     }
18 }

 

Spring Boot Servlet 过滤 监听

标签:creat   自动注册   extends   under   tty   serial   扫描   inter   vax   

原文地址:http://www.cnblogs.com/wangyaobk/p/7452433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!