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生效

  • 相关阅读:
    PostgreSQL中的partition-wise join
    Partition-wise join
    外观模式 门面模式 Facade 结构型 设计模式(十三)
    桥接模式 桥梁模式 bridge 结构型 设计模式(十二)
    组合模式 合成模式 COMPOSITE 结构型 设计模式(十一)
    创建型设计模式对比总结 设计模式(八)
    原型模式 prototype 创建型 设计模式(七)
    单例模式 创建型 设计模式(六)
    建造者模式 生成器模式 创建型 设计模式(五)
    抽象工厂模式 创建型 设计模式(四)
  • 原文地址:https://www.cnblogs.com/shyroke/p/8023881.html
Copyright © 2011-2022 走看看