zoukankan      html  css  js  c++  java
  • (十)SpringBoot之web 应用开发-Servlets, Filters, listeners

    一.需求

    Web 开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 FilterListene

    二、案例

      2.1  通过注册 ServletRegistrationBean、 FilterRegistrationBean 和ServletListenerRegistrationBean 获得

    package com.shyroke;
    
    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;
    
    import util.MyFilter;
    import util.MyListener;
    import util.MyServlet;
    
    @SpringBootApplication
    public class Springboot01Application {
        
        @Bean
        public ServletRegistrationBean servletRegistrationBean() {
            return new ServletRegistrationBean(new MyServlet(), "/servlet");
        }
        
        @Bean
        public FilterRegistrationBean filterRegistrationBean() {
            return new FilterRegistrationBean(new MyFilter(), servletRegistrationBean());
        }
        
        @Bean
        public ServletListenerRegistrationBean<MyListener> listenerRegistrationBean(){
            return new ServletListenerRegistrationBean<MyListener>(new MyListener());
        }
        
        public static void main(String[] args) {
            SpringApplication.run(Springboot01Application.class, args);
        }
    }

      2.2  MyServlet.java

    package util;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    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 {
            System.out.println("我是servlet");
            
            resp.getWriter().write("hello world servlet");
        }
    
        
        
    }

      2.3  MyFilter.java

    package util;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class MyFilter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            System.out.println("我是filter");
            chain.doFilter(request, response);
        }
    
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
            
        }
        
    }

      2.4  MyListener.java

    package util;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    public class MyListener implements ServletContextListener{
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("contextInitialized");
            
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            System.out.println("contextDestroyed");
            
        }
    
    }

      2.5  结果

    说明servlet生效

    filter和listener生效

  • 相关阅读:
    VS中添加搜索路径和链接库的方法
    hive多分隔符支持
    shell 遍历目录下的所有文件
    使用ansible控制Hadoop服务的启动和停止【转】
    Shell中的括号有其特殊的用法
    shell中括号[]的特殊用法 linux if多条件判断
    Linux中rz和sz命令用法详解
    vim 去掉自动注释和自动回车
    ping判断局域网ip使用情况
    shell判断有效日期
  • 原文地址:https://www.cnblogs.com/shyroke/p/8023881.html
Copyright © 2011-2022 走看看