博客
关于我
Spring Boot - Add a Servlet to an Application
阅读量:308 次
发布时间:2019-03-03

本文共 3877 字,大约阅读时间需要 12 分钟。

Spring Boot 环境下的 Servlet 配置与使用

准备

新建 Spring Boot Starter Project

  • 使用 IntelliJ IDEA 创建一个新的 Maven 项目
  • 添加 Spring Boot Starter Web 依赖到 pom.xml 中
  • org.springframework.boot
    spring-boot-starter-web
    1. 新建一个 Servlet 类,位于 src/main/java/com/mk/servlet/HelloServlet.java,内容如下:
    2. package com.mk.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class HelloServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    public HelloServlet() {        System.out.println("HelloServlet.HelloServlet()");    }    @Override    public void init() throws ServletException {        System.out.println("HelloServlet.init()");    }    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        System.out.println("HelloServlet.doGet()");        PrintWriter writer = response.getWriter();        writer.write("Hello, " + request.getRemoteAddr());        writer.flush();        writer.close();    }    @Override    public void destroy() {        System.out.println("HelloServlet.destroy()");    }}

      配置类

      创建一个配置类 ServletComponentConfiguration.java,位于 src/main/java/com/mk/configuration/ServletComponentConfiguration.java,内容如下:

      package com.mk.configuration;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ServletComponentScan(basePackages = {"com.mk.servlet"})public class ServletComponentConfiguration {}

      启动类

      创建一个启动类 SpringBootServletApplication.java,位于 src/main/java/com/mk/SpringBootServletApplication.java,内容如下:

      package com.mk;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringBootServletApplication {    public static void main(String[] args) {        SpringApplication.run(SpringBootServletApplication.class, args);    }}

      通过 Spring Bean 注册 Servlet

      编辑 ServletComponentConfiguration.java,添加以下代码:

      package com.mk.configuration;import java.util.Arrays;import javax.servlet.Servlet;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.mk.servlet.HelloServlet;@Configurationpublic class ServletComponentConfiguration {        @Bean    public ServletRegistrationBean servletRegistration() {        ServletRegistrationBean registration = new ServletRegistrationBean();        registration.setServlet(new HelloServlet());        registration.setUrlMappings(Arrays.asList("/hello"));        return registration;    }}

      启动应用后,控制台会输出:

      HelloServlet.HelloServlet()HelloServlet.init()

      表示 Servlet 已经成功注册到 Spring Boot 容器中。

      使用类路劲扫描注册 Servlet

      编辑 HelloServlet.java,添加 @WebServlet 注解:

      package com.mk.servlet;import java.io.IOException;import java.io.PrintWriter;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(urlPatterns = "/hello")public class HelloServlet extends HttpServlet {    // ... 保持不变}

      编辑 ServletComponentConfiguration.java,删除相关代码,并添加 @ServletComponentScan 注解:

      package com.mk.configuration;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ServletComponentScan(basePackages = {"com.mk.servlet"})public class ServletComponentConfiguration {}

      启动应用后,访问 /hello URL 应该能够正常工作。

      注意事项

    3. HelloServlet.init() 方法会在第一次访问时调用,后续不会重复调用
    4. 使用 @WebServlet 注解可以直接定义 URL 映射路径

    转载地址:http://jhyq.baihongyu.com/

    你可能感兴趣的文章
    Python 中的装饰器是如何工作的,有哪些实际应用场景?
    查看>>
    python读excel
    查看>>
    Python 中读取 CSV 文件-ChatGPT4o作答
    查看>>
    python请求html_使用Python请求获取HTML?
    查看>>
    Python 之匿名函数和偏函数
    查看>>
    python 之栈的实现
    查看>>
    python语音播放
    查看>>
    python语言:装饰器原理
    查看>>
    Python 交互式数据可视化详解
    查看>>
    python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
    查看>>
    Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
    查看>>
    Python 从数据库中存储和检索密码的最安全方法
    查看>>
    Python语言及其应用 - 知识点遍历
    查看>>
    Python 优化提速的 8 个小技巧
    查看>>
    Python 余弦相似度与皮尔逊相关系数 计算
    查看>>
    python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
    查看>>
    python 使用filetype校验文件
    查看>>
    Python 使用flush函数将缓冲区数据立即写磁盘
    查看>>
    python 使用in判断不准确,in不好使
    查看>>
    Python 使用pandas 进行查询和统计详解
    查看>>