17、配置嵌入式servlet容器(1)
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器

1)、如何定制和修改Servlet容器的相关配置
server.port=
server.servlet.context-path=/crud //通用的Servlet容器设置
server.xxx //Tomcat的设置
server.tomcat.xxx
server.tomcat.uri-encoding=UTF-
@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
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);
}
}
@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......
@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)的更多相关文章
- 17. Spring Boot 配置嵌入式Servlet容器
一.如何定制和修改Servlet容器的相关配置 1.配置文件(ServerProperties): 优先级最高 server.port=8081 server.context‐path=/crud s ...
- 配置嵌入式Servlet容器
SpringBoot默认是用的是Tomcat作为嵌入式的Servlet容器:问题?1).如何定制和修改Servlet容器的相关配置:1.修改和server有关的配置(ServerProperties) ...
- 18、配置嵌入式servlet容器(2)
使用其他Servlet容器 -Jetty(长连接) -Undertow(不支持jsp) 替换为其他嵌入式Servlet容器 默认支持: Tomcat(默认使用) Jetty: <depend ...
- 【串线篇】spring boot配置嵌入式servlet容器
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 问题? 一.如何定制和修改Servlet容器的相关配置 1.方法1修改和server有关的配置(ServerProperties ...
- SpringBoot配置嵌入式Servlet容器
1).如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCustomizer]): ...
- 19、配置嵌入式servlet容器(下)
使用外置的Servlet 嵌入式Servlet容器:应用打成可执行的j ar 优点:简单.便携: 缺点:默认不支持JSP.优化定制比较复杂 使用定制器[ServerProperti ...
- Spring boot 配置嵌入式Servlet容器
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCust ...
- springboot(七) 配置嵌入式Servlet容器
github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...
- SpringBoot起飞系列-配置嵌入式Servlet容器(八)
一.前言 springboot中默认使用的是tomcat容器,也叫做嵌入式的servlet容器.因为它和我们平常使用的tomcat容器不一样,这个tomcat直接嵌入到的springboot,平常我们 ...
随机推荐
- 使用Myeclipse进行简单webservice开发的示例
(转) http://blog.csdn.net/changhenshui1990/article/details/70142371 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...
- MySQL:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
错误 原因:可能是服务没有启动 以管理员身份打开cmd 输入 net start mysql
- java读取txt文件,对字符串进行操作后导出txt文件
嘿嘿,代码略为简单,不再多做解释,直接上码! package org.lq.com.util; import java.io.File; import java.io.InputStreamReade ...
- Java温故而知新(4)类String字符串
字符串是由字符组成,在Java中,字符串是对象,是描述字符的基本数据结构.String类可以用来保存一个字符串,本类是最终类,不允许继承: 1.String对象的创建 初始化 由于String对象特别 ...
- C Primer Plus note1
C语言编译错误:multiple definition of `main' main多重定义,在同一个工程中定义了多个main函数 出现如下图的错误: 这是因为在第一张图中,有一个main.c的mai ...
- @media print样式 关于table断页
<html> <head> <style> @media print { table { page-break-after:auto } tr { page-bre ...
- csharp:DropDownComboxTreeView
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using Syste ...
- Django 中间件实现用户认证与IP频率限制
1.URL访问过滤 通过装饰器进行用户认证非常方便,但是在添加部分需要认证的功能时,就需要再次添加装饰器,如果通过中间件来实现,就不需要再进行添加的操作. import re LOGIN_URL = ...
- Python-常用模块1
今天我们来看一看python中的常用的模块,内容有点多,我会分两天来更新这些知识 一.什么是模块 模块就是我们把装有特定功能的代码就行归类的结果,从代码编写的单位来看我们的程序,从小到大的顺序:一条代 ...
- CSS总结摘要
一 概述 1.什么是CSS? Cascading Style Sheet,层叠样式表,用于设定页面内容的显示样式. 2.为一个元素添加多个样式 一个元素可以同时定义多个类,不同类之间用空格隔开,如cl ...