• SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式


    在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式。

    1. 使用Bean的实现方式

    首先创建一个Servlet,一个Filter,一个Listener,

    DemoServlet.java

     

     
    package com.wangx.boot.util.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 DemoServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("get method");
            doPost(req,resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("post method");
            resp.getWriter().write("hello world");
        }
    }
     

      DemoFilter.java

     
    package com.wangx.boot.util.filter;
    
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class DemoFilter 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("开始拦截了....");
        }
    
        @Override
        public void destroy() {
    
        }
    }
     

    DemoListener.java

     
    package com.wangx.boot.util.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    public class DemoListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            System.out.println("contextInitialized");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            System.out.println("contextDestroyed");
        }
    }
     

    然后直接在SpringBoot的启动类中将上面几个方法注册为Bean,

    SpringBootDemo01Application.java
     
    package com.wangx.boot;
    
    import com.wangx.boot.util.filter.DemoFilter;
    import com.wangx.boot.util.listener.DemoListener;
    import com.wangx.boot.util.servlet.DemoServlet;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class SpringBootDemo01Application {
    
        /**
         * 注册Servlet
         * @return
         */
        @Bean
        public ServletRegistrationBean createServlet () {
            //第二个参数为urlPartern
            return new ServletRegistrationBean(new DemoServlet(), "/demoServlet");
        }
    
        /**
         * 注册Filter
         * @return
         */
        @Bean
        public FilterRegistrationBean createFilter() {
            //第二个参数为需要拦截的路径,不传则拦截所有
            return new FilterRegistrationBean(new DemoFilter(), createServlet());
        }
    
        /**
         * 注册Listener
         * @return
         */
        @Bean
        public ServletListenerRegistrationBean createrListener() {
            //自定义个Listener.监听整个Context的Servlet
            return new ServletListenerRegistrationBean(new DemoListener());
        }
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootDemo01Application.class, args);
        }
    }
     

      启动项目时会Listener会打印contextInitialized,关闭时会打印contextDestroyed,访问时拦截器打印"开始拦截了....",并成功访问到Servlet,并输入hello world到浏览器上。

    2. 实现ServletContextInitializer接口

      直接在SpringBoot的启动类中实现ServletContextInitializer,重写onStartup()方法也可以使用Servlet.Filter,Listener,以上面三个Demo类为例,使用ServletContextInitializer的方式的代码:

     
    package com.wangx.boot;
    
    import com.wangx.boot.util.filter.DemoFilter;
    import com.wangx.boot.util.listener.DemoListener;
    import com.wangx.boot.util.servlet.DemoServlet;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletContextInitializer;
    
    import javax.servlet.DispatcherType;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import java.util.EnumSet;
    
    
    @SpringBootApplication
    public class SpringBootDemo01Application implements ServletContextInitializer {
        public static void main(String[] args) {
            SpringApplication.run(SpringBootDemo01Application.class, args);
        }
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            //创建Servlet,并映射访问路径为demoServlet
            servletContext.addServlet("demoServlet", new DemoServlet()).addMapping("/demoServlet");
            //创建Filter,拦截的Servlet
            servletContext.addFilter("demoFilter", new DemoFilter()).addMappingForServletNames(EnumSet.of(DispatcherType.FORWARD), true, "demoServlet"); //创建Listener servletContext.addListener(new DemoListener()); } }
     

      运行结果与1相同

    3. 使用注解的方式

      这是一个SpringBoot官方推荐的方式,也是三种方式中最简单的方式,只需要在SpringBoot的启动类上添加@ServletComponentScan注解,在DemoServlet类上添加@WebServlet(name = "demoServlet", urlPatterns = "/demoServlet")注解,DemoFilter类上添加@WebFilter(urlPatterns = "/*", dispatcherTypes = DispatcherType.FORWARD)(拦截所有请求),DemoListener类上添加@WebListener即可,运行结果与1相同。

    原文  SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式

  • 相关阅读:
    Odoo中的Widget
    Odoo中的domain
    Odoo中的模型详解
    Odoo中的模型继承、视图继承、Qweb模板继承详解
    Odoo权限控制详解
    Odoo字段类型详解
    WSGI——python web 服务器网关接口
    Backbone——数据驱动UI的js开发模式
    Underscore——JS函数库
    Werkzeug——python web开发工具包
  • 原文地址:https://www.cnblogs.com/xiaoshen666/p/10843986.html
Copyright © 2020-2023  润新知