博客
关于我
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 一个list去除另一个list中的值
    查看>>
    python 三大框架的 介绍。
    查看>>
    Python 下载的 11 种姿势,一种比一种高级!
    查看>>
    python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
    查看>>
    Python 中 3 个不可思议的返回功能
    查看>>
    python 中 dict 的另一种用法
    查看>>
    Python 中 PIL 读取图片出现异常旋转的解决方法
    查看>>
    Python 中只有一个 True 和一个 False 对象吗?
    查看>>
    python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
    查看>>
    Python 中多线程与多处理之间的区别
    查看>>
    Python 中如何使用 lambda 函数
    查看>>
    Python 中如何创建多行字符串?
    查看>>
    Python 中如何处理异常?
    查看>>
    Python 中如何实现列表的切片?
    查看>>
    Python 中如何实现字典的排序?
    查看>>
    Python 中常用的数据类型及相关操作详解
    查看>>
    Python 中的离线语音转文本
    查看>>
    Python 中的线程
    查看>>
    Python 中的继承机制是什么样的?
    查看>>
    Python 中的装饰器是什么?
    查看>>