
Spring Boot - Add a Servlet to an Application
发布日期:2021-05-06 23:02:54
浏览次数:10
分类:技术文章
本文共 5101 字,大约阅读时间需要 17 分钟。
文章目录
准备
新建 Spring Starter Project:
编辑 pom.xml 文件,引入 spring-boot-starter-web
等依赖:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.mk spring-boot-servlet 1.0.0 spring-boot-servlet 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-maven-plugin org.springframework.boot spring-boot-configuration-processor org.projectlombok lombok
新建一个 Servlet 类:
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()"); }}
新建一个 Servlet 配置类:
package com.mk.configuration;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.Configuration;@Configurationpublic class ServletComponentConfiguration { }
启动类:
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
配置类,注册 Servlet 组件(第 16 ~ 24 行):
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 ServletRegistrationBeanservletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean<>(); registration.setServlet(new HelloServlet()); registration.setUrlMappings(Arrays.asList("/hello")); return registration; }}
启动应用,观察控制台输出:
HelloServlet.HelloServlet()
说明 Spring Boot 已经将 HelloServlet
添加到容器中。
访问 ,看到返回预期的内容,表示 Servlet 组件添加成功:
注意:第一次访问 的过程中,
HelloServlet.init()
方法会被调用,此后不再被调用。
通过使用类路劲扫描添加 Servlet
编辑 HelloServlet
类:
- 使用
@WebServlet
注解,设置urlPatterns
属性指定 URL 映射路径(第 12 行)
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
配置类:
- 删除第 10 ~ 18 行
- 使用
@ServletComponentScan
注解,设置basePackages
属性指定 Servlet 组件的扫描路径(第 7 行)
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 { // @Bean// public ServletRegistrationBeanservletRegistration() { // ServletRegistrationBean registration = new ServletRegistrationBean<>();// // registration.setServlet(new HelloServlet());// registration.setUrlMappings(Arrays.asList("/hello"));// // return registration;// }}
启动应用,访问 ,看到返回预期的内容,表示 Servlet 组件添加成功。
参考
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月12日 16时22分39秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
花书读书笔记(十九)-深度生成模型
2019-03-04
《百面机器学习》读书笔记(一)-特征工程
2019-03-04
《凸优化》中科大-讲解 -系列笔记(汇总55/55)
2019-03-04
STL教程:C++ STL快速入门(非常详细)
2019-03-04
MySQL中索引与视图的用法与区别详解
2019-03-04
【论文泛读03】卷积LSTM网络:一种短时降雨量预测的机器学习方法
2019-03-04
中科大-凸优化 笔记(lec45)-强凸性等价不等式
2019-03-04
linux 中 alien命令的使用
2019-03-04
【论文泛读29】关系抽取:卷积神经网络的视角
2019-03-04
shell 中的 set命令 -e -o 选项作用
2019-03-04
Python中JSON的基本使用
2019-03-04
函数的默认参数值,即在定义参数的时候给它一个默认值
2019-03-04
c++流迭代器的一个错误和分析(第二段代码)
2019-03-04
ubuntu install baidu inputmethod
2019-03-04
程序员建议(忘记从哪里转的了,反正是csdn上的一个兄弟)
2019-03-04
电脑重装系统后提示invalid partition table怎么解决
2019-03-04
c++ primer 5th 练习11.9自己编写的答案
2019-03-04
web实现断点续传
2019-03-04
自定义BootstrapTable扩展:分页跳转到指定页码
2019-03-04
Python3逻辑运算符
2019-03-04