• SpringBoot 注册Servlet三大组件【Servlet、Filter、Listener】-原生代码+@Bean+效果展示


    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。
    注册三大组件,class MyServlet()/class MyListener/ class MyFilter 需要自己亲自编写!!

    1. class MyServlet

    package com.szs.springboot04.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 MyServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().print("Hello MyServlet!!!");
        }
    }
    

    ServletRegistrationBean 注册

    @Configuration
    public class MyServerConfig {
        /**
         * 注册三大组件
         */
        @Bean
        public ServletRegistrationBean myServlet(){
            ServletRegistrationBean registrationBean =
                    new ServletRegistrationBean(new MyServlet(),"/myServlet");
            registrationBean.setLoadOnStartup(1);
            return registrationBean;
        }
    

    效果展示


    2.class MyFilter implements Filter

    import javax.servlet.*;
    import java.io.IOException;
    
    public class MyFilter implements Filter {
        public MyFilter() {
            super();
        }
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("myFilter正在初始化...");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("MyFilter 正在Process...");
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    

    FilterRegistrationBean 注册

    @Configuration
    public class MyServerConfig {
      @Bean
        public FilterRegistrationBean myFilter(){
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            registrationBean.setFilter(new MyFilter());
            registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
            return registrationBean;
        }
    



    3.class MyListener implements ServletContextListener

    public class MyListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            System.out.println("contextInitialized ...web应用启动");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            System.out.println("contextDestroyed... web项目已经销毁");
        }
    }
    

    ServletListenerRegistrationBean myListener()

    @Configuration
    public class MyServerConfig {
    @Bean
    public ServletListenerRegistrationBean myListener(){
            return  new ServletListenerRegistrationBean<>(new MyListener());
        }
    
    


  • 相关阅读:
    .NET对象克隆的深究(转)
    25条哈佛成功金言 (转)
    __doPostBack()方法研究
    一个好的人事博客
    (转)IIS的inetinfo.exe进程占用CPU达100%解决办法
    .net开发随笔
    visual studio.net已检测到web服务器运行的不是asp.net1.1版"故障的排除
    Infragistics NetAdvantage控件的使用:该关系不是此 DataView 指向的表的父关系
    新的一年开始了
    六种方法,做一名更好的开发者
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/10460404.html
Copyright © 2020-2023  润新知