20. Spring Boot Servlet【从零开始学Spring Boot】

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 注解自动注册,无需其他代码。

这里我们新建一个spring-boot-hello2java工程进行测试;这里不过多进行说明,如果这个还不会的话,请回到上上上一章进行查看。

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

  1. com.kfit.servlet.MyServlet1:
  2. package com.kfit.servlet;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. /**
  11.  *
  12.  *@author Angel(公众号:SpringBoot)
  13.  *@version v.0.1
  14.  */
  15. //这个不需要添加.
  16. //@WebServlet(urlPatterns="/myServlet1/*", description="Servlet的说明")
  17. public class MyServlet1 extends HttpServlet{
  18.       
  19.        private static final long serialVersionUID = 1L;
  20.        @Override
  21.    protected void doGet(HttpServletRequestreq, HttpServletResponse resp) throwsServletException, IOException {
  22.         System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");
  23.         doPost(req,resp);
  24.    }
  25.    @Override
  26.    protected void doPost(HttpServletRequestreq, HttpServletResponse resp) throwsServletException, IOException {
  27.         System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");
  28.         resp.setContentType("text/html"); 
  29.         PrintWriter out = resp.getWriter(); 
  30.         out.println("<html>"); 
  31.         out.println("<head>"); 
  32.         out.println("<title>HelloWorld</title>"); 
  33.         out.println("</head>"); 
  34.         out.println("<body>"); 
  35.         out.println("<h1>这是:MyServlet1</h1>"); 
  36.         out.println("</body>"); 
  37.         out.println("</html>");
  38.    }
  39. }

com.kfit.App中注册:

package com.kfit;

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.boot.context.embedded.ServletRegistrationBean;
  4. import org.springframework.boot.web.servlet.ServletComponentScan;
  5. import org.springframework.context.annotation.Bean;
  6.  
  7. import com.kfit.servlet.MyServlet1;
  8.  
  9. /**
  10.  *
  11.  *
  12.  *大家也许会看到有些demo使用了3个注解:@Configuration
  13.  *
  14.  *@EnableAutoConfiguration
  15.  * @ComponentScan
  16.  *
  17.  *                        其实:@SpringBootApplication申明让spring boot自动给程序进行必要的配置,
  18.  *
  19.  *               等价于以默认属性使用@Configuration
  20.  *                @EnableAutoConfiguration@ComponentScan
  21.  *
  22.  *所以大家不要被一些文档误导了,让自己很迷茫了,希望本文章对您有所启发;
  23.  *
  24.  * @authorAngel(QQ:412887952)
  25.  * @version v.0.1
  26.  */
  27. @SpringBootApplication
  28. public class App {
  29.       
  30.        /**
  31.         *注册Servlet.不需要添加注解:@ServletComponentScan
  32.         * @return
  33.         */
  34.        @Bean
  35.        publicServletRegistrationBean MyServlet1(){
  36.               return new ServletRegistrationBean(new MyServlet1(),"/myServlet/*");
  37.        }
  38.       
  39.        public static void main(String[] args) {
  40.               SpringApplication.run(App.class,args);
  41.        }
  42. }





右键Run As JavaApplication进行访问http://127.0.0.1:8080/myServlet1

使用注解注册Servlet示例代码

com.kfit.servlet.MyServlet2.java

  1. package com.kfit.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.  *
  14.  * @authorAngel(QQ:412887952)
  15.  * @version v.0.1
  16.  */
  17. @WebServlet(urlPatterns="/myServlet2/*",description="Servlet的说明")
  18. public class MyServlet2 extends HttpServlet{
  19.       
  20.        private static final long serialVersionUID = 1L;
  21.  
  22.        @Override
  23.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
  24.        System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");
  25.         doPost(req, resp);
  26.     }
  27.  
  28.     @Override
  29.     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
  30.        System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");
  31.         resp.setContentType("text/html"); 
  32.         PrintWriter out =resp.getWriter(); 
  33.        out.println("<html>"); 
  34.        out.println("<head>"); 
  35.        out.println("<title>Hello World</title>"); 
  36.        out.println("</head>"); 
  37.         out.println("<body>"); 
  38.        out.println("<h1>这是:myServlet2</h1>"); 
  39.        out.println("</body>"); 
  40.        out.println("</html>");
  41.     }
  42. }

SpringBootSampleApplication.java

  1. package com.kfit;
  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//这个就是扫描相应的Servlet包;
  13. public class SpringBootSampleApplication {
  14.  
  15.     public static void main(String[]args) {
  16.         SpringApplication.run(SpringBootSampleApplication.class, args);
  17.     }
  18. }

访问http://127.0.0.1:8080/myServlet2

购买完整视频,请前往:http://www.mark-to-win.com/TeacherV2.html?id=287