SpringBoot2.x怎么自定义servlet的三大组件

1. 自定以servlet,继承HttpServlet 并重写doGet、doPost方法

/**
 * 自定义的servlet
 */
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello java SpringBoot");
    }
}

2.自定义filter过滤器,实现Javax.Servlet.*包中的Filter接口,重写其方法


/**
 * 自定义filter过滤器
 */
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

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

    @Override
    public void destroy() {

    }
}

3.实现自定义的监听器,实现想要自定义的监听器 例如这里实现的是容器的初始化和销毁的监听器。


/**
 * ServletContext监听器
 */
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized...bean:初始化了");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextInitialized...bean:当前项目销毁了");
    }
}

4.把自定义的三大组件,分别注册到容器中···

/**
 * 专门配置一个配置server的类 包括:
 * Server配置,包括可以配置tomca
 *
 * Server的三大组件,Serverlet、Lisnter、fiflter
 */
@Configuration
public class MyServerConfig {
    /**
     *springboot2 使用的和springboot1.x 不一样
     * 添加定制器ConfigurableServletWebServerFactory
     *
     * 配置嵌入式的Servlet容器,springboot的嵌入式容器就是tomcat
     */
    @Bean
    public ConfigurableServletWebServerFactory configurableTomcatWebServerFactory(){
        TomcatServletWebServerFactory factory=new TomcatServletWebServerFactory();
        factory.setPort(8081);
        return factory;
    }

    /**
     *把在定义的MyServlet注册到springboot容器里
     *
     * 返回ServletRegistrationBean对象
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        //FIXME 疑问:为什么要自定义Servlet spring不是有自己的DispicharServlet吗
        ServletRegistrationBean<MyServlet> registrationBean = new ServletRegistrationBean<>(new MyServlet(),"/MyServlet");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
    /**
     * 把自定义的filter注册到容器中
     */
    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello", "/MyServlet"));
        return registrationBean;
    }
    /**
     * 把自定义的监听器注册到容器中
     */
    @Bean
    public ServletListenerRegistrationBean<MyListener> listenerRegistrationBean(){
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }
}

你可能感兴趣的:(整理一点点,随记)