Spring Boot Day7 Servlet

一、配置嵌入式Servlet容器

1.1 Spring Boot默认的Servlet

Spring Boot默认使用Tomcat作为嵌入式的Servlet
默认是在webapp/WEB-INF/web.xml下注册

1.2 定制和修改Servlet容器相关配置

  • 修改和Server有关的配置
  • 编写一个WebServerFactoryCustomizer:嵌入式的Servlet容器的定制器;来修改Servlet容器的
    配置

在MymvcConfig类中配置:

public WebServerFactoryCustomizer<ConfigurableWebServerFactory> myCustomizer(){
    return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
        @Override
        public void customize(ConfigurableWebServerFactory factory) {
            factory.setPort(8081);
        }
    };
}

1.3 注册Servlet

public class myServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("你好呀,詹欧缓释");
    }
}

@Bean
public ServletRegistrationBean myServlet(){
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(new myServlet(), "/myServlet");
    return registrationBean;
}

1. 4 注册过滤器

public class myfilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("过滤器执行");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> myCustomizer(){
    return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
        @Override
        public void customize(ConfigurableWebServerFactory factory) {
            factory.setPort(8081);
        }
    };
}

1.5 注册监听器

public class myListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("web应用启动");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("web应用销毁");

    }
}

@Bean
public ServletListenerRegistrationBean myListener(){
    ServletListenerRegistrationBean<myListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean(new myListener());
    return  servletListenerRegistrationBean;
}

1.6 默认拦截

  • “/"
    所有请求,包括静态资源。但是不拦截jsp请求
  • 修改前端控制器默认拦截的请求的路径
spring.mvc.servlet.path="/"

二、使用其他Servlet容器

2.1 去除tomcat依赖,添加其他依赖

  • jetty
    支持长链接
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <version>2.1.5.RELEASE</version>
</dependency>
  • undertow
    不支持jsp
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
    <version>2.1.5.RELEASE</version>
</dependency>

2.2 使用外置的Servlet容器

  • 嵌入式Servlet容器:应用打成可执行的jar
    优点:简单、便携;
    缺点:默认不支持JSP、优化定制比较复杂(使用定制器【ServerProperties、自定义
    EmbeddedServletContainerCustomizer】,自己编写嵌入式Servlet容器的创建工厂
    【EmbeddedServletContainerFactory】);

2.3 步骤

外置的Servlet容器:外面安装Tomcat—应用war包的方式打包;

  • 必须创建一个war项目;(利用idea创建好目录结构)
  • 将嵌入式的Tomcat指定为provided;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐tomcat</artifactId>
<scope>provided</scope>
</dependency> 
  • 必须编写一个SpringBootServletInitializer的子类,并调用configure方法
public class ServletInitializer extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		//传入SpringBoot应用的主程序
		return application.sources(SpringBoot04WebJspApplication.class);
	}
} 
  • 启动服务器就可以使用

2.4 原理

  • jar包:执行SpringBoot主类的main方法,启动ioc容器,创建嵌入式的Servlet容器;
  • war包:启动服务器,服务器启动SpringBoot应用【SpringBootServletInitializer】,启动ioc容器;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值