在idea新建项目

springboot整合servlet_spring

 

springboot整合servlet_spring_02

 

springboot整合servlet_ide_03

 

 springboot整合servlet_ide_04

 

这个是pom.xml文件需要添加的依赖包

springboot整合servlet_ide_05

 

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

 

 新建一个servlet包

 springboot整合servlet_java_06

 

在这个包下面新建FirstServlet类

 springboot整合servlet_ide_07

 

FirstServlet.java 类

package com.example.gong02.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
* springboot 整合servlet方式一
*
<servlet>
* <servlet-name>FirstServlet</servlet-name>
* <servlet-class>com.bjsxt.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 {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("FistServlet..........");
}
}

 

 

编写启动类

springboot整合servlet_spring_08

 

 App.java类

package com.example.gong02;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* SpringBoot 整合 Servlet 方式一
* *
*/

@SpringBootApplication

@ServletComponentScan
//在 springBoot 启动时会扫描@WebServlet, 并将该类实例化 

public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

 

 

运行App.java

springboot整合servlet_ide_09

 

springboot整合servlet_ide_10

 

 

 在浏览器输入

springboot整合servlet_java_11

终端打印

springboot整合servlet_spring_12

 

 

下面介绍servlet的第二种整合方式

新建SecondServlet类

springboot整合servlet_ide_13

 

 springboot整合servlet_java_14

package com.example.gong02.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* SpringBoot 整合 Servlet 方式二
*
*/
public class SecondServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("SecondServlet.......");
}
}

 

 

编写启动类App2.java

springboot整合servlet_java_15

 

 App2.java类

package com.example.gong02;

import com.example.gong02.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
* SpringBoot 整合 Servlet 方式二
* *
*/
@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;
}
}

 

 

运行

springboot整合servlet_ide_16

 

 springboot整合servlet_spring_17

 

 在浏览器输入

springboot整合servlet_ide_18

 

可以看到终端打印出来了

 springboot整合servlet_ide_19

 

 

 注意了,大家在运行方式二的时候可能会出现8080端口被占用的情况导致tomcat无法启动,遇到这种情况可以重启一下idea就能解决了