1 整合Servlet 方式一
1.1通过注解扫描完成Servlet 组件的注册
1.1.1创建Servlet
/**
* 整合Servlet 方式一
*/
@WebServlet(name = "FirstServlet",urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response){
System.out.println("First Servlet........");
}
}
1.1.2修改启动类
@SpringBootApplication
@ServletComponentScan//在spring Boot 启动时会扫描@WebServlet
注解,并将该类实例化
public class SpringbootwebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootwebApplication.class,
args);
}
}
2 整合Servlet 方式二
2.1通过方法完成Servlet 组件的注册
2.1.1创建Servlet
/**
* 整合Servlet 方式二
*/
public class SecondServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response){
System.out.println("Second Servlet........");
}
}
2.1.2创建Servlet 配置类
/**
* Servlet 配置类
*/
@Configuration
public class ServletConfig {
/**
* 完成Servlet 组件的注册
*/
@Bean
public ServletRegistrationBean
getServletRegistrationBean(){
ServletRegistrationBean bean = new
ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
}