在SpringBoot中,有一个默认的嵌入式Servlet容器——Tomcat容器
1、如何定制和修改Servlet容器的有关配置?
【1】在SpringBoot的配置文件中进行配置
# 修改服务器的端口号
server.port=8090
# 修改访问路径
server.servlet.context-path=/webDemo
# 设置Tomcat容器的字符编码
server.tomcat.uri-encoding=UTF-8
# server.tomcat.xxx= 是对tomcat容器的设置
# server.xxx.xxx= 是通用的servlet容器设置
【2】手动编写一个嵌入式的Servlet容器的定制器
① 1.x版本:使用EmbeddedServletContainerCustomizer
来修改Servlet容器的配置
@Configuration
public class MyMvcConfiguration extends WebMvcConfigurerAdapter {
//在容器中配置嵌入式Servlet容器
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
return new EmbeddedServletContainerCustomizer(){
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8888);
}
};
}
}
② 2.x版本:使用ConfigurableServletWebServerFactory
来修改Servlet容器的配置
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory(){
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(8010);
return factory;
}
2、在SpringBoot中注册三大组件(Servlet、Filter、Listener)
【1】1.x版本
① 注册Servlet
在容器中注册自定义的Servlet:
@Configuration
public class MyServerConfiguration{
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return servletRegistrationBean;
}
}
自定义的Servlet:
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.write("Hello,MyServlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
【2】注册过滤器Filter
在容器中注册自定义的过滤器:
@Configuration
public class MyServerConfiguration {
/*注册过滤器Filter组件*/
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/myServlet","/hello"));
return filterRegistrationBean;
}
}
自定义的过滤器:
public class MyFilter implements Filter {
/* *
* @description //TODO 过滤器的初始化方法
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/*
* @description //TODO 过滤器的执行方法
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("MyFiler is processing……");
chain.doFilter(servletRequest,servletResponse);
}
/* 过滤器的销毁方法*/
@Override
public void destroy() {
}
}
【3】注册监听器(Listener)
在容器中注册监听器:
@Bean
public ServletListenerRegistrationBean myServletListener(){
ServletListenerRegistrationBean<MyServletListener> servletRegistrationBean = new ServletListenerRegistrationBean<MyServletListener>(new MyServletListener());
return servletRegistrationBean;
}
自定义的监听器:
public class MyServletListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("web应用启动……");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("web容器销毁……");
}
}
可以注册的监听器:
static {
Set<Class<?>> types = new HashSet();
types.add(ServletContextAttributeListener.class);
types.add(ServletRequestListener.class);
types.add(ServletRequestAttributeListener.class);
types.add(HttpSessionAttributeListener.class);
types.add(HttpSessionListener.class);
types.add(ServletContextListener.class);
SUPPORTED_TYPES = Collections.unmodifiableSet(types);
}
SpringBoot会帮助我们自动注册Spring MVC的前端控制器 (DispatcherServlet)
默认拦截(/):除jsp请求外,拦截所有的请求,包括静态资源;(/*):会拦截jsp请求。
可以通过server.servletPath
来修改SpringMVC前端控制器默认拦截的请求路径。
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public ServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet, this.serverProperties.getServletMapping());
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
【2】2.x版本注册方式与上面的相同。
3、使用其他Servlet容器
在SpringBoot中,默认使用Tomcat服务器,但是也可以切换成·Undertow和Jetty。
如何切换其他容器?
【1】先排除SpringBoot的默认容器Tomcat
<!--导入web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
【2】引入目标容器的依赖
① 切换成undertow容器:
<!--切换其他容器
1、先排除默认的tomcat容器
2、引入要切换的容器的座标
-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<!--<version>SpringBoot的版本号</version> -->
</dependency>
② 切换成Netty容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>版本号</version>
</dependency>