Spring boot整合Servlet

 1 @WebServlet(name="FirstServlet",urlPatterns="/first")
 2 public class FirstServlet extends HttpServlet {
 3     @Override
 4     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
 5             throws ServletException, IOException {
 6         System.out.println("FirstServlet..........");
 7         
 8     }
 9     
10 }
11 
12 @SpringBootApplication
13 @ServletComponentScan //在spring boot启动时会扫描@WebServlet,并将该类实例化
14 public class App {
15 
16     public static void main(String[] args) {
17         SpringApplication.run(App.class, args);
18 
19     }
20 
21 }

public class SecondServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("secondServlet.........");
    }

}
@SpringBootApplication
public class App2 {
    public static void main(String[] args) {
        SpringApplication.run(App2.class, args);
    }
    @Bean
    public ServletRegistrationBean getBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
}

 

  

 

posted @ 2018-08-02 15:05  大GG女孩  阅读(144)  评论(0)    收藏  举报