SpringBoot默认使用Tomcat作为嵌入式的Servlet容器

server.port=8088 server.servlet.context-path=/crud //通用的Servlet容器设置 server.xxx //Tomcat的设置 server.tomcat.xxx server.tomcat.uri-encoding=UTF-8
@Configuration public class config { @Bean public WebServerFactoryCustomizer webServerFactoryCustomizer(){ return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory >() { //定制嵌入式的Servlet容器相关的规则 @Override public void customize(ConfigurableServletWebServerFactory factory) { factory.setPort(8087); } }; } }
 
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 = "/";
 


原文:https://www.cnblogs.com/Mrchengs/p/10357470.html