SpringBoot이 서브렛을 통합하는 두 가지 방법

1704 단어
  • 프로젝트pom파일Jar패키지 가입
    
            org.springframework.boot
            spring-boot-starter-web
        
    1.메모 검색 방법 - 서브렛 준비
    @WebServlet(name = "firstServlet", urlPatterns = "/firstServlet")  //   servlet,       。
    public class FirstServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().append("firstServlet");
        }
    
    }
    
    - 서브렛 등록
    @SpringBootApplication
    @ServletComponentScan   //      ,             webservlet   
    public class FirstServletApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FirstServletApplication.class, args);
        }
    }
    
    2.등록 방법 구성 - 서브렛 준비
    //       webServlet  
    public class SecondServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().append("SecondServlet");}}
    
    - 서브렛 등록
    @SpringBootApplication
    public class SecondServletApplication {
        public static void main(String[] args) {
            SpringApplication.run(SecondServletApplication.class, args);
        }
        @Bean  //    ,          
        public ServletRegistrationBean getServletRegistrationBean() {  //     ServletRegistrationBean
            ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());     //     Servlet    
            bean.addUrlMappings("/secondServlet");  //     
            return bean;}}
    
  • 좋은 웹페이지 즐겨찾기