首页 > 编程语言 > 详细

2、SpringBoot整合之SpringBoot整合servlet

时间:2021-07-03 22:27:39      阅读:22      评论:0      收藏:0      [点我收藏+]

SpringBoot整合servlet

一、创建SpringBoot项目,仅选择Web模块即可

技术分享图片
技术分享图片
技术分享图片
技术分享图片

二、在POM文件中添加依赖

<!-- 添加servlet依赖模块 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- 添加jstl标签库依赖模块 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

三、创建servlet,通过注解@WebServlet指定name和url

项目结构如下

技术分享图片

package cn.byuan.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;

@WebServlet(name = "testOneServlet", urlPatterns = "/test_one")// 通过注解@WebServlet指定name和url
public class ServletTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("Hello, servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

四、在启动类上添加注解,指定扫描servlet所在的包

package cn.byuan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan("cn.byuan.servlet")// 指定扫描servlet所在的包
public class Test002SpringbootServletApplication {

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

}

五、启动项目进行测试

测试地址:http://localhost:8080/test_one

技术分享图片

源码地址:https://github.com/byuan98/springboot-integration/tree/master/test002_springboot_servlet

2、SpringBoot整合之SpringBoot整合servlet

原文:https://www.cnblogs.com/byuan/p/14966849.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!