1. 通过注解扫描完成Servlet组件的注册(SpringBoot整合Servlet方式一)
1.1. 使用maven构建SpringBoot的名叫spring-boot-servlet项目
1.2. pom.xml
<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>
<groupId>com.bjbs</groupId>
<artifactId>spring-boot-servlet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<!-- 修改jdk版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
1.3. 编写FirstServlet.java
package com.bjbs1.controller;
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;
/**
* SpringBoot整合Servlet方式一
*
* <servlet>
* <servlet-name>FirstServlet</servlet-name>
* <servlet-class>com.bjsxt1.servlet.FirstServlet</servlet-class>
* </servlet>
*
* <servlet-mapping>
* <servlet-name>FirstServlet</servlet-name>
* <url-pattern>/first</url-pattern>
* </servlet-mapping>
*/
@WebServlet(name = "FirstServlet", urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("FirstServlet............");
}
}
1.4. @WebServlet注解
1.5. 编写App.java启动类
package com.bjbs1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* SpringBoot整合Servlet方式一, SpringBoot启动类
*/
@SpringBootApplication
@ServletComponentScan // 在springBoot启动时会扫描@WebServlet, 并将该类实例化
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
1.6. 配置@ServletComponentScan注解, 在SpringBoot启动时会扫描@WebServlet, 并将该类实例化
1.7. 运行项目, 并用浏览器访问FirstServlet
2. 通过方法完成Servlet组件的注册(SpringBoot整合Servlet方式二)
2.1. 编写SecondServlet.java
package com.bjbs2.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* SpringBoot整合Servlet方式二
*/
public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("SecondServlet............");
}
}
2.2. 编写App2.java启动类
package com.bjbs2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.bjbs2.controller.SecondServlet;
/**
* SpringBoot整合Servlet方式二, SpringBoot启动类
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Bean
public ServletRegistrationBean getServletRegistrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
}
2.3. 使用org.springframework.boot.web.servlet.ServletRegistrationBean类注册Servlet
2.4. 运行项目, 并用浏览器访问SecondServlet