SpringBoot默认使用的是嵌入式的Servlet容器(Tomcat)
方法1. 修改server相关配置(ServerProperties类中的属性)
server.port=8088
server.servlet.context-path=/tt
server.tomcat.uri-encoding=UTF-8
//通用Servlet容器设置
server.xxx
//tomcat设置
server.tomcat.xxx
方法2:编写一个EmbeddedServletContainerCustomizer
SpringBoot 2.0 以后改成了 WebServerFactoryCustomizer
//配置嵌入式servlet服务器
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
//定制嵌入式容器相关规则
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8888);
}
};
}
以下方法:
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("MyServlet");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
//注册三大组件
//@Configuration public class MyServerConfig { 中
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return registrationBean;
}
public class MyFilter implements javax.servlet.Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("MyFilter doFilter");
filterChain.doFilter(servletRequest,servletResponse);
}
}
//@Configuration public class MyServerConfig { 中
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("contextInitialized 应用启动了.....");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("contextDestroyed 应用销毁了.....");
}
}
//@Configuration public class MyServerConfig { 中
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> listenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return listenerRegistrationBean;
}
SpringBoot帮我们自动配置SpringMVC的时候,自动注册了SpringMVC的前端控制器DispatcherServlet
@Bean(
name = {"dispatcherServletRegistration"}
)
@ConditionalOnBean(
value = {DispatcherServlet.class},
name = {"dispatcherServlet"}
)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
//默认拦截: / 所有资源 包括静态资源 不包括.jsp /* 包括.jsp
//可以通过server.servlet.context-path 来修改前端控制器默认拦截的请求路径
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.webMvcProperties.getServlet().getPath());
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
Jetty(长连接)
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcatartifactId>
<groupId>org.springframework.bootgroupId>
exclusion>
exclusions>
dependency>
2.pom.xml 引入其他Servlert容器
<dependency>
<artifactId>spring-boot-starter-jettyartifactId>
<groupId>org.springframework.bootgroupId>
dependency>
Undertow (不支持jsp)
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcatartifactId>
<groupId>org.springframework.bootgroupId>
exclusion>
exclusions>
dependency>
2.pom.xml 引入其他Servlert容器
<dependency>
<artifactId>spring-boot-starter-undertowartifactId>
<groupId>org.springframework.bootgroupId>
dependency>