springboot中使用servlet原生相关功能

写在前面

本文介绍在springboot中使用servlet原生相关功能的方式,主要有两种:

  • 注解方式
    这种方式依赖于@WebServlet@WebFilter,@WebListner等注解,需要注意,这些注意是在servlet3.0为了实现无web.xm(注意:这是个人根据学习的猜测!!!)正式引入的,因此想要使用的话必须是servlet3.0及以上版本
  • bean configuration方式
    这种方式通过springboot的XXXBean完成相关组件的注册。

好的,那我们就正式开始吧!!!

1:项目创建

  • ide
    我们最爱的idea
  • 构建工具
    曾经的王者,现在逐渐被gradle取代的maven
  • jdk
    至今还没有被后来者撼动地位的jdk8
  • springboot
    2.2.10.RELEASE

1.1:new->project

选择左侧菜单中的Spring Initilizr,starter选择default
springboot中使用servlet原生相关功能_第1张图片

1.2:录入GAV

根据个人录入GAV,选择java8,项目构建为`jar,等。
springboot中使用servlet原生相关功能_第2张图片

1.3:选择依赖

主要选择web下spring-web。springboot版本选择2.2.10
springboot中使用servlet原生相关功能_第3张图片

1.4:finish

项目名称根据个人决定是否修改,正常不用管,和ArtifactId保持一致。
springboot中使用servlet原生相关功能_第4张图片

2:注解方式

2.1:定义servlet

@WebServlet("/user/servlet")
public class MyAnnotationServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        resp.getWriter().write("user servlet");
    }
}

2.2:定义filter

@WebFilter("/user/servlet")
public class MyAnnotationFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
        FilterChain filterChain) throws IOException, ServletException {
        System.out.println("income log filter " + servletRequest.getRemoteHost());
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

2.3:定义servletcontextlistener监听器

@WebListener
public class MyAnnotationServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("app startup at " + LocalDateTime.now().toString());
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}

2.4:启动类增加@ServletComponentScan注解

@SpringBootApplication
@ServletComponentScan
public class SpringbootTestservletApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTestservletApplication.class, args);
    }

}

2.5:启动并访问测试

  • 启动
2020-09-29 15:36:38.732  INFO 4720 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1957 ms
app startup at 2020-09-29T15:36:38.791
  • 访问测试
    在这里插入图片描述

3:bean configuration方式

3.1:定义servlet

public class MyBeanConfigurationServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        resp.getWriter().write("this is springboot servlet by bean configuration");
    }
}

3.2:定义filter

public class MyBeanConfigurationFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
        FilterChain filterChain) throws IOException, ServletException {
        System.out.println("spring boot filter by bean configuration");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }
}

3.3:定义servletcontextlistener

public class MyBeanConfigurationServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("springboot servlet context listener by bean configuration started!");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}

3.4:定义bean configuration

@SpringBootConfiguration
public class MyBeanConfiguration {

    // 注册servlet
    @Bean
    public ServletRegistrationBean createMyBeanConfigurationServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
            new MyBeanConfigurationServlet(), "/book.do");
        return servletRegistrationBean;
    }

    // 注册filter
    @Bean
    public FilterRegistrationBean createMyBeanConfigurationFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyBeanConfigurationFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/book.do"));
        return filterRegistrationBean;
    }

    // 注册监听器
    @Bean
    public ServletListenerRegistrationBean createMyBeanConfigurationServletContextListener() {
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(new MyBeanConfigurationServletContextListener());
        return servletListenerRegistrationBean;
    }
}

3.5:启动并测试

  • 启动
2020-09-29 15:57:18.374  INFO 18772 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1891 ms
springboot servlet context listener by bean configuration started!
  • 测试
    在这里插入图片描述

4:源码

点击这里。

最后:都让开,我要喝瑞幸

在这里插入图片描述

你可能感兴趣的:(springboot,java,spring,spring,boot)