• (十)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生效

  • 相关阅读:
    PCI Express体系结构导读(3)- PCIE
    PCI Express体系结构导读(2)
    npm添加代理和取消代理
    流媒体压力测试rtmp&hls(含推流和拉流)
    流媒体跳坑: 03-视频会议:使用LiveRTC来做视频直播
    流媒体跳坑: 02-视频会议:Webrtc服务器
    正交调制解调(MATLAB)
    mp4文件断电保存-(关于:MP4视频文件损坏的一点想法)
    流媒体跳坑: 01-Mp4 文件解析
    centos 守护 node 进程
  • 原文地址:https://www.cnblogs.com/shyroke/p/8023881.html
Copyright © 2020-2023  润新知