[Spring Boot] 使用多个Servlet
当使用Spring boot的嵌入式servlet容器时,可以通过Spring bean或扫描Servlet组件的方式注册Servlet、Filter和Servlet规范的所有监听器(例如HttpSessionListener)
- 当urlMapping不是很复杂时,可以通过
ServletRegistrationBean、FilterRegistrationBean和ServletListenerRegistrationBean获得完整控制。如果bean实现了ServletContextInitializer接口的话则可以直接注册。 - 当使用
@ServletComponentScan扫描Servlet组件时,Servlet、过滤器和监听器可以是通过@WebServlet、@WebFilter和@WebListener自动注册
Application.java
package com.yqu.multiservlet; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.DispatcherServlet; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} @Bean
public ServletRegistrationBean dispatcherRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration =
new ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings("/hirest/*");
printStacks();
return registration;
} @Bean
public ServletRegistrationBean servletRegistrationBean() {
printStacks();
return new ServletRegistrationBean(
new SigninServlet(), "/signin");
} private void printStacks() {
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
System.out.println("========================"); for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
}
}
}
SigninServlet.java
package com.yqu.multiservlet; import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; public class SigninServlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
super.init(config); } protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("http://blog.sina.com.cn/yandongqu");
}
}
HelloController.java
package com.yqu.multiservlet; import org.springframework.hateoas.ResourceSupport;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; @RestController
public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public HttpEntity home() {
ResourceSupport home = new ResourceSupport();
home.add(linkTo(methodOn(HelloController.class).home()).withSelfRel());
return new ResponseEntity(home, HttpStatus.OK);
}
}
application.properties
server.context-path=/HelloMultiServlet
server.port=8080 applicationDefaultJvmArgs: [
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=55558"
]
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'hello-multiservlet'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-web")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("org.springframework.hateoas:spring-hateoas")
compile("org.springframework.plugin:spring-plugin-core:1.1.0.RELEASE")
compile("com.jayway.jsonpath:json-path:0.9.1")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
测试
- 通过REST访问http://localhost:8080/HelloMultiServlet/hirest/

[Spring Boot] 使用多个Servlet的更多相关文章
- Spring Boot → 08:嵌入式Servlet容器自定义
Spring Boot → 08:嵌入式Servlet容器自定义
- SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - 自定义Servlet、Filter、Listener是如何注册到Tomcat容器中的?(SpringBoot实现SpringMvc的原理)
上一篇我们讲了SpringBoot中Tomcat的启动过程,本篇我们接着讲在SpringBoot中如何向Tomcat中添加Servlet.Filter.Listener 自定义Servlet.Filt ...
- 【串线篇】spring boot使用外置的Servlet容器
嵌入式Servlet容器:应用打成可执行的jar 优点:简单.便携: 缺点:默认不支持JSP.优化定制比较复杂 (使用定制器[ServerProperties/自定义EmbeddedServletCo ...
- Spring boot 注册Filter , Listener, Servlet
1: ServletRegistrationBean Servlet @Bean public ServletRegistrationBean myServlet(){ ServletRegist ...
- Spring Boot 知识笔记(servlet、监听器、拦截器)
一.通过注解自定义servlet package net.Eleven.demo.servlet; import javax.servlet.ServletException; import java ...
- spring boot 与servlet
servlet: 基于java的web组件,用于生成动态内容,由容器管理. 类似其他java技术组件,由平台无关的java类组成,并且由java web服务器加载执行 serv ...
- spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)
spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...
- Spring Boot使用Servlet、Filter或Listener的方式
根据官方文档说明,有两种方式可以在你的Spring Boot应用中使用Servlet.Filter或Listener. 其一:将Servlet.Filter或Listener注册成Spring Bea ...
- Spring boot中注册Servlet
Spring boot中注册Servlet 如何在spring boot项目中注册Servlet呢? 如何在spring boot项目中注册Servlet呢? 由于没有web.xml,无法直接在xml ...
随机推荐
- Jquery 给Js动态新添加的元素 绑定的点击事件
//one $('.class').on("click",function(){ alert('one') }); //相当于$('.class').bind("clic ...
- 一个风控计算负载过高到mysql主从拆分暴露的各种设计复杂性问题以及解决方法总结
在很多系统(包括金融类和非金融类)中,其实有大量的系统在很长的一段时间内(具体多长时间视业务的成功与否而定)都是混合型系统,也就是同时具有OLTP+OLAP的业务.我们说任何形式的存在在特定阶段都是合 ...
- 05: jQuery
目录: jQuery参考网站 W3school 1.1 JQuery作用 1.2 jQuery与DOM比较 与 相互转换 1.3 jQuery选择器 1.4 jQuery筛选与过滤 1.5 jQuer ...
- 【前端】javascript+jquery实现手风琴式的滚动banner或产品展示图
实现效果 实现步骤 // 鼠标放入到li中该盒子变宽,其他盒子变窄,鼠标移开大盒子,恢复原样 // 实现步骤 // 1. 给li添加背景 // 2. 绑定onmouseover事件,鼠标放入到li中, ...
- Python3基础 print \n换行
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 多线程中的信号机制--signwait()函数【转】
本文转载自:http://blog.csdn.net/yusiguyuan/article/details/14237277 在Linux的多线程中使用信号机制,与在进程中使用信号机制有着根本的区别, ...
- attr返回被选元素的属性值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [IDEA插件] - 一个不错的插件
今天看到微信平台一篇推送IDEA插件的文章继而下载了个插件看了下. 名字叫做codehelper.generator codehelper.generator http://plugins.jetbr ...
- 接口中带参方法,传入IB类型的数据
不同的接口有不同的方法 不同的类有不同的作用 不同的作用产生不一样的效果 不同的效果让程序看似复杂,实际简单... 比如此程序,看似复杂,实际就那么点事: 谁生成了谁,谁设置了谁,谁传入了谁,谁被谁调 ...
- C#学习笔记(六):循环嵌套、复杂数据类型和枚举
复杂数据类型 默认情况:0,1,2,3 赋值情况:0,3,4,5://修改初始值,后面都会改变 定义在class外面,作用域更大 定义在class里面(类种类),只能在类里使用 枚举作用:方便把不同角 ...