1.通过注解扫描完成SpringBoot整合Servlet组件
创建Maven项目
编写pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<groupId>cn.bl</groupId>
<artifactId>02-springboot-servlet1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
编写Servlet和启动类
package cn.bl.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="HelloSpringBootServlet",value="/hello")//替代web.xml的配置
public class HelloSpringBootServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doget...");
}
}
package cn.bl.servlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//扫描servlet
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
执行上面的main方法,访问:http://localhost:8080/hello
控制台输出doget...即为访问成功了
2.通过方法完成SpringBoot整合Servlet组件
继续编写第二个Servlet和启动类
这种方法也就是不写上一个方法的注解,改为下面的@Bean的方法,访问:http://localhost:8080/hello2
package cn.bl.servlet2;
import javax.servlet.http.HttpServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public ServletRegistrationBean<HttpServlet> getServletRegistrationBean(){
ServletRegistrationBean<HttpServlet> bean = new ServletRegistrationBean<>(new HelloSpringBootServlet());
bean.addUrlMappings("/hello2");
return bean;
}
}
package cn.bl.servlet2;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloSpringBootServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doget2..");
}
}