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,平常我们 ...
随机推荐
- [转]Web API Introduction to OData Services using ASP.NET Web API
本文转自:http://mahedee.net/tag/web-api/ What is OData? OData Stands for Open Data Protocol. It is a dat ...
- [android] 天气app布局练习(二)
主要练习一下GridView MainActivity.java package com.example.weatherreport; import java.util.ArrayList; impo ...
- Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(一:知识点回顾)
一.知识点回顾 1.Mybatis环境搭建(DAO层的实现)(使用maven项目管理工具) 需要引入的依赖包: <!-- 单元测试junit --> <dependency> ...
- PAT 1044. Shopping in Mars
#include <cstdio> #include <cstdlib> #include <vector> #include <climits> #i ...
- sql中replace的用法
update 表名 set 字段名=REPLACE (字段名,'原来的值','要修改的值') 如:将tbl_user表的user_name字段中的大写的A替换成小写的a update tbl_stud ...
- js实现数组内数据的上移和下移
var swapItems = function(arr, index1, index2){ arr[index1] = arr.splice(index2,1,arr[index1])[0] ret ...
- Java Struts2 (二)
二.封装请求正文到对象中(非常重要) 1.静态参数封装 在struts.xml配置文件中,给动作类注入值.调用的是setter方法. 原因:是由一个staticParams的拦截器完成注入的. 2.动 ...
- Keras 时序模型
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Thinking_boy1992/article/details/53207177 本文翻译自 时序模 ...
- Spring Boot 整合 Thymeleaf 完整 Web 案例
Thymeleaf 是一种模板语言.那模板语言或模板引擎是什么?常见的模板语言都包含以下几个概念:数据(Data).模板(Template).模板引擎(Template Engine)和结果文档(Re ...
- idea 出现 java.lang.OutOfMemoryError: PermGen space
今天在项目启动时候,刚刚启动 就 报了 Exception in thread "http-bio-8080-exec-1" 之后 出现了 java.lang.OutOfMemor ...