当使用Spring boot的嵌入式servlet容器时,可以通过Spring bean或扫描Servlet组件的方式注册Servlet、Filter和Servlet规范的所有监听器(例如HttpSessionListener)

  • 当urlMapping不是很复杂时,可以通过ServletRegistrationBeanFilterRegistrationBean 和 ServletListenerRegistrationBean获得完整控制。如果bean实现了ServletContextInitializer接口的话则可以直接注册。
  • 当使用@ServletComponentScan扫描Servlet组件时,Servlet、过滤器和监听器可以是通过@WebServlet@WebFilter@WebListener自动注册

Application.java

  1. package com.yqu.multiservlet;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.context.embedded.ServletRegistrationBean;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.servlet.DispatcherServlet;
  8.  
  9. @SpringBootApplication
  10. public class Application {
  11.  
  12. public static void main(String[] args) {
  13. SpringApplication.run(Application.class, args);
  14. }
  15.  
  16. @Bean
  17. public ServletRegistrationBean dispatcherRegistration(
  18. DispatcherServlet dispatcherServlet) {
  19. ServletRegistrationBean registration =
  20. new ServletRegistrationBean(dispatcherServlet);
  21. registration.addUrlMappings("/hirest/*");
  22. printStacks();
  23. return registration;
  24. }
  25.  
  26. @Bean
  27. public ServletRegistrationBean servletRegistrationBean() {
  28. printStacks();
  29. return new ServletRegistrationBean(
  30. new SigninServlet(), "/signin");
  31. }
  32.  
  33. private void printStacks() {
  34. StackTraceElement[] elements = Thread.currentThread().getStackTrace();
  35. System.out.println("========================");
  36.  
  37. for (int i = 0; i < elements.length; i++) {
  38. System.out.println(elements[i]);
  39. }
  40. }
  41. }

SigninServlet.java

  1. package com.yqu.multiservlet;
  2.  
  3. import javax.servlet.ServletConfig;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9.  
  10. public class SigninServlet extends HttpServlet {
  11. public void init(ServletConfig config)
  12. throws ServletException {
  13. super.init(config);
  14.  
  15. }
  16.  
  17. protected void doGet(
  18. HttpServletRequest request,
  19. HttpServletResponse response)
  20. throws ServletException, IOException {
  21. response.sendRedirect("http://blog.sina.com.cn/yandongqu");
  22. }
  23. }

HelloController.java

  1. package com.yqu.multiservlet;
  2.  
  3. import org.springframework.hateoas.ResourceSupport;
  4. import org.springframework.http.HttpEntity;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import org.springframework.web.bind.annotation.RestController;
  11.  
  12. import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
  13. import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
  14.  
  15. @RestController
  16. public class HelloController {
  17. @RequestMapping(value = "/", method = RequestMethod.GET)
  18. @ResponseBody
  19. public HttpEntity home() {
  20. ResourceSupport home = new ResourceSupport();
  21. home.add(linkTo(methodOn(HelloController.class).home()).withSelfRel());
  22. return new ResponseEntity(home, HttpStatus.OK);
  23. }
  24. }

application.properties

  1. server.context-path=/HelloMultiServlet
  2. server.port=8080
  3.  
  4. applicationDefaultJvmArgs: [
  5. "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=55558"
  6. ]

build.gradle

  1. buildscript {
  2. repositories {
  3. mavenCentral()
  4. }
  5. dependencies {
  6. classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
  7. }
  8. }
  9.  
  10. apply plugin: 'java'
  11. apply plugin: 'eclipse'
  12. apply plugin: 'idea'
  13. apply plugin: 'spring-boot'
  14.  
  15. jar {
  16. baseName = 'hello-multiservlet'
  17. version = '0.1.0'
  18. }
  19.  
  20. repositories {
  21. mavenCentral()
  22. }
  23.  
  24. sourceCompatibility = 1.8
  25. targetCompatibility = 1.8
  26.  
  27. dependencies {
  28. compile("org.springframework.boot:spring-boot-starter-actuator")
  29. compile("org.springframework.boot:spring-boot-starter-web")
  30. compile("com.fasterxml.jackson.core:jackson-databind")
  31. compile("org.springframework.hateoas:spring-hateoas")
  32. compile("org.springframework.plugin:spring-plugin-core:1.1.0.RELEASE")
  33. compile("com.jayway.jsonpath:json-path:0.9.1")
  34. }
  35.  
  36. task wrapper(type: Wrapper) {
  37. gradleVersion = '2.3'
  38. }

测试

  • 通过REST访问http://localhost:8080/HelloMultiServlet/hirest/

[Spring Boot] 使用多个Servlet的更多相关文章

  1. Spring Boot → 08:嵌入式Servlet容器自定义

    Spring Boot → 08:嵌入式Servlet容器自定义

  2. SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - 自定义Servlet、Filter、Listener是如何注册到Tomcat容器中的?(SpringBoot实现SpringMvc的原理)

    上一篇我们讲了SpringBoot中Tomcat的启动过程,本篇我们接着讲在SpringBoot中如何向Tomcat中添加Servlet.Filter.Listener 自定义Servlet.Filt ...

  3. 【串线篇】spring boot使用外置的Servlet容器

    嵌入式Servlet容器:应用打成可执行的jar 优点:简单.便携: 缺点:默认不支持JSP.优化定制比较复杂 (使用定制器[ServerProperties/自定义EmbeddedServletCo ...

  4. Spring boot 注册Filter , Listener, Servlet

    1: ServletRegistrationBean   Servlet @Bean public ServletRegistrationBean myServlet(){ ServletRegist ...

  5. Spring Boot 知识笔记(servlet、监听器、拦截器)

    一.通过注解自定义servlet package net.Eleven.demo.servlet; import javax.servlet.ServletException; import java ...

  6. spring boot 与servlet

    servlet:      基于java的web组件,用于生成动态内容,由容器管理.      类似其他java技术组件,由平台无关的java类组成,并且由java web服务器加载执行   serv ...

  7. spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)

    spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...

  8. Spring Boot使用Servlet、Filter或Listener的方式

    根据官方文档说明,有两种方式可以在你的Spring Boot应用中使用Servlet.Filter或Listener. 其一:将Servlet.Filter或Listener注册成Spring Bea ...

  9. Spring boot中注册Servlet

    Spring boot中注册Servlet 如何在spring boot项目中注册Servlet呢? 如何在spring boot项目中注册Servlet呢? 由于没有web.xml,无法直接在xml ...

随机推荐

  1. 优化 MySQL: 3 个简单的小调整

    我并不期望成为一个专家级的 DBA,但是,在我优化 MySQL 时,我推崇 80/20 原则,明确说就是通过简单的调整一些配置,你可以压榨出高达 80% 的性能提升.尤其是在服务器资源越来越便宜的当下 ...

  2. 如何写出一个让人很难发现的bug?

    程序员的日常三件事:写bug.改bug.背锅.连程序员都自我调侃道,为什么每天都在加班?因为我的眼里常含bug. 那么如何写出一个让(坑)人(王)很(之)难(王)发现的bug呢? - 1 -新手开发+ ...

  3. SQL去重之distinct和group by的应用

    遇到一个需求,要去重查出某张表的字段一和字段二,但是查出来的结果要按照表中记录的创建时间排序. 于是,第一时间就想到了使用distinct这个去重专用语法了: select distinct col1 ...

  4. js面向对象编程: js类定义函数时prototype和this区别?

    参考文章的链接:http://www.2cto.com/kf/201406/307790.html 测试代码如下: function ListCommon2(afirst) { var first=a ...

  5. cogs 2223. [SDOI2016 Round1] 生成魔咒

    ★★☆ 输入文件:menci_incantation.in 输出文件:menci_incantation.out 简单对比 时间限制:1 s 内存限制:128 MB [题目描述]魔咒串由许多魔咒字符组 ...

  6. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解

    题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...

  7. Linux上Java的安装与配置

    由于使用 yum 或者 apt-get 命令 安装 openjdk 可能存在类库不全,从而导致用户在安装后运行相关工具时可能报错的问题,所以此处我们推荐采用手动解压安装的方式来安装 JDK.具体步骤如 ...

  8. spring boot application.properties/application.yml 配置属性大全

    来自官网  https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.h ...

  9. Visual Studio 项目模板制作(一)

    我们编写项目的时候,很多时候都是在写重复代码,比如一个比较完整的框架,然后下面有很多代码都是重复的Copy,其实我们可以利用Visual Studio的模板替我们干这些活,我们只要关注项目具体的业务就 ...

  10. java代码实现highchart与数据库数据结合完整案例分析(二)---折线图

    作者原创:未经博主允许不许转载 在上一篇的博客中,展示和分析了如何做一个饼状图,有疑问可以参考上一篇博客. 现在分析和展示折线图的绘制和案例分析, 先展示效果图: 与饼状图不同的是,折线图展现更多的数 ...