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,平常我们 ...
随机推荐
- awk中引用变量使用单引号''
举例如下 who命令输出第一列 (1)第一种情况不使用引号 # i=1;who | awk '{print $${i}}' 输出如下: awk: {print $${i}} awk: ...
- 卸载或安装程序出现:The feature you are trying to use is on a network resource ...
卸载或安装程序出现:The feature you are trying to use is on a network resource ... 这种情况可能是因为原先已经安装过这个软件,所以要先卸载 ...
- How to limit Dialog's max height?
1. We can make it to play trick in code. At Dialog's show function, after app has set contentView, w ...
- [翻译]Review——The Inner Workings Of Virtual DOM
The Inner Workings Of Virtual DOM 虚拟DOM的内部工作机制 原文地址:https://medium.com/@rajaraodv/the-inner-workings ...
- JS加法相关
1:首先JS是一种弱语言,但是同类型可以自己相加减 例如“a”+”b” 可以自动组成ab : 1+ 2 自动变成3 var data = 2; var currentPage = data; //2 ...
- csharp: Getting all image files in folder
/// <summary> /// /// </summary> /// <param name="sender"></param> ...
- 记录开发Nodejs c++ addon的一些经验(一、技术栈)
Nodejs c++ addon 是用c++去编写Nodejs的插件 技术栈: 1.node-gyp 一个用于把c++文件编译成node可执行文件的库 2.v8 google v8引擎 用于处理c++ ...
- 弹性布局(flex)
一.Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为 Flex 布局.但在使用时 ...
- Java 文件上传与下载、email
1. 文件上传与下载 1.1 文件上传 文件上传,要点: 前台: 1. 提交方式:post 2. 表单中有文件上传的表单项: <input type="file" /> ...
- CentOS 7运维管理笔记(12)----GUI配置工具Webmin的安装
早期的Linux系统管理员或是Web管理员在修改服务器配置时使用最多的就是vi编辑器,但是现在越来越多的基于GUI界面的配置工具出现了,毕竟人们还是喜欢以直接的可视化的方式来修改服务器的配置,而不是再 ...