zoukankan      html  css  js  c++  java
  • SpringBoot注册Servlet Filter Listener三大组件

    转自:https://www.jianshu.com/p/6830989985e9

    1.注册Servlet

    配置类:

    @Configuration
    public class MyServerConfig {
        // 注册三大组件
        @Bean
        public ServletRegistrationBean myServlet(){
            ServletRegistrationBean servletregistrationbean =  new ServletRegistrationBean(new MyServlet() ,"/myservlet"){
            };
            return servletregistrationbean;
        }
        // 配置嵌入式Servlet容器
        @Bean
        public WebServerFactoryCustomizer<ConfigurableWebServerFactory> myembeddedServletContainerCustomizer(){
            return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>(){
                // 定制嵌入式Servlet容器相关规则
                @Override
                public void customize(ConfigurableWebServerFactory factory) {
                    factory.setPort(8081);
                }
            };
        }
    }
    

    自定义的Servlet类

    public class MyServlet extends HttpServlet {
        /**
         * 处理get请求
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }
        /**
         * 处理post请求
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("hello");
        }
    }
    

    然后在浏览器访问http://localhost:8081/myservlet

     
    image.png

    2.注册Filter

        // 注册filter
        @Bean
        public FilterRegistrationBean myfilter(){
            FilterRegistrationBean filterregistrationbean = new FilterRegistrationBean();
            filterregistrationbean.setFilter(new MyFilter());
            filterregistrationbean.setUrlPatterns(Arrays.asList("/hello","/myservlet"));
            return filterregistrationbean;
        }
    
    public class MyFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void destroy() {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("myfilter");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    

    然后访问"/myservlet"或者"/hello"控制台都会打印myfilter


     
    image.png

    3.注册Listener(这里以ServletContextListener为例)

        // 注册Listener
        @Bean
        public ServletListenerRegistrationBean mylistener(){
            ServletListenerRegistrationBean servletListener = new ServletListenerRegistrationBean<MyListener>(new MyListener());
            return servletListener;
        }
    
    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项目销毁");
        }
    }
    

    然后启动项目


     
    image.pn


    作者:二营长家的张大炮
    链接:https://www.jianshu.com/p/6830989985e9
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    画出直线,找出直线经过的图像坐标
    MFC 对话框图片上,鼠标拖动画矩形框
    MFC知识点整理
    mfc +opencv 读取图片显示到对话框
    python 获取系统时间,新建时间目录
    在Ubuntu下后台持续运行Python程序
    利用conda安装tensorflow
    整理的最全 python常见面试题(基本必考)
    转:Django框架基础知识(面试题)
    shell 获取变量是什么数据类型
  • 原文地址:https://www.cnblogs.com/sharpest/p/13706494.html
Copyright © 2011-2022 走看看