Servlet注册三大组件Servlet、Filter、Listener

根据Spring boot 的web开发中,我们会用到Servlet、Filter、Listener的注册,如果我们的项目中有

webapp/WEB-INF,里面会存在web.xml的配置文件,Spring Boot默认是以jar包的方式来启动嵌入式的Servlet容器,以此来启动SpringBoot的web应用,因此没有web.xml文件,但是在Spring boot没有情况下,我们也可以换一种方式进行注册

注册Servlet、Filter、Listener我们会用到对应的ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean

 

1、注册Servlet

先写一个Servlet的类继承HttpServlet 

/**
 * @description:
 * @author: 
 * @create: 2020-01-27 10:24
 **/
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("Hello MyServlet");
    }
}

然后注册自定义的Servlet,主要实现ServletRegistrationBean 类

该类的构造方法是传入Servlet,和需要拦截的URL

 public ServletRegistrationBean(T servlet, String... urlMappings) {
        this(servlet, true, urlMappings);
    }
/**
 * @description:
 * @author: 
 * @create: 2020-01-27 10:27
 **/

@Configuration
public class MyServerConfig {

    //注册三大组件
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        return registrationBean;
    };

}

浏览器进行访问:http://localhost:8089/myServlet

则会输出:Hello MyServlet

在web开发中会涉及到一个前端控制器,在我们加入SpringMVC的时候,自动配置了前端控制器DispatcherServlet

需要注意的一点是他的拦截,默认拦截的是  /  代表拦截所有请求,包括静态资源,但是不拦截jsp请求  当我们/*这样写

才会拦截jsp请求,如果需要修改拦截的请求路径可以通过配置文件中的server.servletPath来实现

2、注册Filter

注册Filter,编写自定义的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("========processing=======");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

和servlet同样的注册方式

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

3、Listener

实现ServletContextListener 

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> myServletServletRegistrationBean = new ServletListenerRegistrationBean<MyListener>(new MyListener());
        return  myServletServletRegistrationBean;
    }

 

发布了19 篇原创文章 · 获赞 0 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章