• Springboot整合Servlet


    1,方式一

      【1】通过注解扫描完成Servlet组件的注册

    @WebServlet(name = "firstServlet",urlPatterns = "/first")
    public class firstServlet extends HttpServlet {
    
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("FirstServlet...");
        }
    }

      【2】修改启动类

    @SpringBootApplication
    @ServletComponentScan//在Spring Boot启动时会扫描@WebServlet注解,并将该类实例
    public class SpringbootwebApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootwebApplication.class, args);
        }
    
    }

    2,方法二

        【1】通过方法完成Servlet组件的注册

    public class SecondServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("SecondServlet...");
        }
    }

        【2】创建Servlet配置类

    @Configuration
    public class ServletConfig {
    
        @Bean
        public ServletRegistrationBean getServletRegistrationBean()
        {
            ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
            bean.addUrlMappings("/second");
            return bean;
        };
    }
  • 相关阅读:
    AES加解密
    redis HyperLogLog使用
    vi常用操作
    redis string使用
    用jdk命令定位java应用问题
    用户态、内核态及零拷贝
    缓存穿透与雪崩
    ReentrantLock、Semaphore、AQS
    redis list使用
    不同数据库取并集、交集、差集
  • 原文地址:https://www.cnblogs.com/yz-bky/p/12690667.html
Copyright © 2020-2023  润新知