博客
关于我
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/

    你可能感兴趣的文章
    PHP获取IP所在地区(转)
    查看>>
    PHP获取IP的方法对比
    查看>>
    php获取json里面内容
    查看>>
    R2的版本由来
    查看>>
    PHP获取图片宽度高度、大小尺寸、图片类型、用于布局的img属性
    查看>>
    PHP获取当前时间、时间戳的各种格式写法汇总
    查看>>
    PHP获取当前页面的完整URL
    查看>>
    php获取文件夹中文件的两种方法
    查看>>
    PHP获取日期的一些方法总结
    查看>>
    R2学习记录
    查看>>
    PHP获取本周的每一天的时间
    查看>>
    php获取网页内容的三种方法
    查看>>
    R-CNN算法优化策略
    查看>>
    PHP规范PSR0和PSR4的理解
    查看>>
    php解析ipa包,获取logo
    查看>>
    php设置socket超时时间
    查看>>
    php设计模式 萨莱 pdf,PHP设计模式 建造者模式
    查看>>
    php设计模式之装饰器模式
    查看>>
    R&Python Data Science系列:数据处理(5)--字符串函数基于R(一)
    查看>>
    PHP设计模式:观察者模式
    查看>>