SpringBoot 注册Servlet三大组件ServletFilterListener-原生代码+@Bean+效果展示

Posted 好好学习,天天向上

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 注册Servlet三大组件ServletFilterListener-原生代码+@Bean+效果展示相关的知识,希望对你有一定的参考价值。

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。
注册三大组件,class MyServlet()/class MyListener/ class MyFilter 需要自己亲自编写!!

1. class MyServlet

package com.szs.springboot04.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

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().print("Hello MyServlet!!!");
    }
}

ServletRegistrationBean 注册

@Configuration
public class MyServerConfig {
    /**
     * 注册三大组件
     */
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean =
                new ServletRegistrationBean(new MyServlet(),"/myServlet");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }

效果展示


2.class MyFilter implements Filter

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {
    public MyFilter() {
        super();
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("myFilter正在初始化...");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("MyFilter 正在Process...");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

FilterRegistrationBean 注册

@Configuration
public class MyServerConfig {
  @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }



3.class MyListener implements ServletContextListener

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

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("contextDestroyed... web项目已经销毁");
    }
}

ServletListenerRegistrationBean myListener()

@Configuration
public class MyServerConfig {
@Bean
public ServletListenerRegistrationBean myListener(){
        return  new ServletListenerRegistrationBean<>(new MyListener());
    }


以上是关于SpringBoot 注册Servlet三大组件ServletFilterListener-原生代码+@Bean+效果展示的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot-注册web三大组件

java_day29_Servlet三大组件之监听器listener

spring boot 2 - webrestfulCrud实验错误处理机制配置嵌入式Servlet容器

小编带着小白看springboot源码7

带着萌新看springboot源码07

010 注册web组件