SpringBoot默认使用Tomcat作为嵌入式的Servlet容器

1)、如何定制和修改Servlet容器的相关配置

        1、修改和server有关的配置
           (ServerProperties【也是EmbeddedServletContainerCustomizer】)
server.port=
server.servlet.context-path=/crud //通用的Servlet容器设置
server.xxx //Tomcat的设置
server.tomcat.xxx
server.tomcat.uri-encoding=UTF-
        2、编写一个EmbeddedServletContainerCustomizer:
            嵌入式的Servlet容器的定制器;来修改Servlet容器的配置
 
         在Spring Boot2.0以上配置嵌入式Servlet容器时
         EmbeddedServletContainerCustomizer类不存在,
         经网络查询发现被WebServerFactoryCustomizer替代
@Configuration
public class config {
@Bean
public WebServerFactoryCustomizer webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory >() { //定制嵌入式的Servlet容器相关的规则
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort();
}
};
}
}

2)、注册Servlet、Filter、Listener

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动
SpringBoot的web应用,没有web.xml文件
 
使用一下三个类:
ServletRegistrationBean
FilterRegistrationBean
ServletListenerRegistrationBean
注册Servlet
Servlet实现类:
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
}
 
ServletRegistrationBean实现Servl加入容器
@Configuration
public class MyServletConfig {
//注册三大组件
@Bean
public ServletRegistrationBean servletRegistrationBean(){
// public ServletRegistrationBean(T servlet, String... urlMappings)
ServletRegistrationBean re = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return re;
}
}

/myServlet、是拦截浏览器的请求

 注册Filter

public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("MyFilter...");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}

将Filter添加到容器

@Configuration
public class MyServletConfig {
//注册Filter
@Bean
public FilterRegistrationBean filterRegistrationBean(){
// public FilterRegistrationBean(T filter, ServletRegistrationBean... servletRegistrationBeans)
FilterRegistrationBean f = new FilterRegistrationBean();
f.setFilter(new MyFilter());
f.setUrlPatterns(Arrays.asList("/myfilter","/test"));
return f;
}
}

Listener:

public class MyListener implements ServletContextListener {
//监听当前web项目销毁
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("web end....");
}
//启动监听
@Override
public void contextInitialized(ServletContextEvent sce) { System.out.println("web start...");
}
}

加入容器  

@Configuration
public class MyServletConfig {
//注册Listener
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean<MyListener> L = new
ServletListenerRegistrationBean<MyListener>(new MyListener());
return L;
}
}

同时可以设置的参数很多如load-starton......

SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;
DIspatcherServlet;DispatcherServletAutoConfiguration中:
@Bean(
name = {"dispatcherServletRegistration"}
)
@ConditionalOnBean(
value = {DispatcherServlet.class},
name = {"dispatcherServlet"}
)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.webMvcProperties.getServlet().getPath());
//默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求
// /*会拦截jsp
//可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
} return registration;
}

getPath->WebMvcProperties ->  private String path = "/";

17、配置嵌入式servlet容器(1)的更多相关文章

  1. 17. Spring Boot 配置嵌入式Servlet容器

    一.如何定制和修改Servlet容器的相关配置 1.配置文件(ServerProperties): 优先级最高 server.port=8081 server.context‐path=/crud s ...

  2. 配置嵌入式Servlet容器

    SpringBoot默认是用的是Tomcat作为嵌入式的Servlet容器:问题?1).如何定制和修改Servlet容器的相关配置:1.修改和server有关的配置(ServerProperties) ...

  3. 18、配置嵌入式servlet容器(2)

    使用其他Servlet容器 -Jetty(长连接) -Undertow(不支持jsp) 替换为其他嵌入式Servlet容器   默认支持: Tomcat(默认使用) Jetty: <depend ...

  4. 【串线篇】spring boot配置嵌入式servlet容器

    SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 问题? 一.如何定制和修改Servlet容器的相关配置 1.方法1修改和server有关的配置(ServerProperties ...

  5. SpringBoot配置嵌入式Servlet容器

    1).如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCustomizer]): ...

  6. 19、配置嵌入式servlet容器(下)

    使用外置的Servlet   嵌入式Servlet容器:应用打成可执行的j ar 优点:简单.便携: 缺点:默认不支持JSP.优化定制比较复杂         使用定制器[ServerProperti ...

  7. Spring boot 配置嵌入式Servlet容器

    SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCust ...

  8. springboot(七) 配置嵌入式Servlet容器

    github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...

  9. SpringBoot起飞系列-配置嵌入式Servlet容器(八)

    一.前言 springboot中默认使用的是tomcat容器,也叫做嵌入式的servlet容器.因为它和我们平常使用的tomcat容器不一样,这个tomcat直接嵌入到的springboot,平常我们 ...

随机推荐

  1. 原创经验:微信小程序开发总结

    学习时间不短了.今天公司不加班总结一下我的开发经验吧,以下都是我认为很重要的总结哦!写下来让我自己也记得更清楚,同时希望可以帮助到有需要的同学哦 一: 参数传值的方法 1:  data-id我们可以给 ...

  2. Swift数据类型_整型和浮点型

    //swift中的整型和浮点型 /** * //类型推断整数是Int 浮点数是Double ,日常使用需要注意不能越界,存储时间毫秒数 英雄经验数等等之类内容容易越界 整型 大多数情况下,你不需要在代 ...

  3. 如何通过 PHP 获取 Azure Active Directory 令牌

    在调用 Azure Rest API 时,如果是属于 Azure Resource Manager 的 API,则需要使用 Azure Active Directory (Azure AD)认证获取令 ...

  4. window.open()被浏览器拦截问题汇总

    一.问题描述 最近在做项目的时候碰到了使用window.open被浏览器拦截的情况,虽然在自己的环境可以对页面进行放行,但是对用户来说,不能要求用户都来通过拦截.何况当出现拦截时,很多用户根本不知道发 ...

  5. 小任务之使用SVG画柱状图~

    function drawBar(data) { var barGraph = document.querySelector("#bar-graph"); var graphWid ...

  6. thinkphp5设置404页面不跳转

    thinkphp5设置404页面的步骤: 1. 首先关闭调试模式,即配置application/config文件,使'app_debug' => false 2. 添加自定义404页面跳转地址, ...

  7. [转]Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager

    转自:http://www.cnblogs.com/oppoic/p/6165581.html Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存 ...

  8. svg拖拽和缩放

    需求:做机房平面图,用svg实现拖拽和缩放,刚开始一头雾水,不知所措,好在皇天不负有心人........ 本文重点介绍拖拽,单纯实现很简单,但是由于vue项目,机房图有很多事件,拖拽就成了难点 简单介 ...

  9. 实现绘制图形的ToolBar

    给地图添加绘制图形的ToolBar还是有必要的,比较人性化的功能.图形的样式可以自己定制,也提供了朴实的默认样式.对 dojo 不太懂,出现了许许多多问题,真是蛋疼的一天啊.令人惊喜的是 ArcGis ...

  10. OpenCV中Mat属性step,size,step1,elemSize,elemSize1

    Mat的step,size,step1,elemSize,elemSize1这几个属性非常容易混淆. OpenCV的官方参考手册也没有解释清楚这几个概念. 前一段时间研究了一下每个属性的含义,如果有什 ...