Spring Boot 中的Servlet简单使用
作者:牛头人 时间:2023-01-28 03:08:06
当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有 * (如HttpSessionListener * )。
Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?
在spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(Filter和Listener也是如此)。
一、代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。
也可以通过实现 ServletContextInitializer 接口直接注册。
二、在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。
1.通过代码注册Servlet示例代码
1).SpringBootSimpleApplication.Java类:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.example.servlet.MyServlet;
@SpringBootApplication
public class SpringBootSimpleApplication {
/**
* 使用代码注册Servlet(不需要@ServletComponentScan注解)
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new MyServlet(), "/st/*");// ServletName默认值为首字母小写,即myServlet
}
public static void main(String[] args) {
SpringApplication.run(SpringBootSimpleApplication.class, args);
}
}
2).MyServlet.java类:
package com.example.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 MyServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>大家好,我的名字叫Servlet</h1>");
out.println("</body>");
out.println("</html>");
}
}
2.使用注解注册Servlet示例代码
1).SpringBootSimpleApplication.java类:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.example.servlet.MyServlet;
@SpringBootApplication
@ServletComponentScan
public class SpringBootSimpleApplication {
/**
* 使用代码注册Servlet(不需要@ServletComponentScan注解)
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new MyServlet(), "/st/*");// ServletName默认值为首字母小写,即myServlet
}
public static void main(String[] args) {
SpringApplication.run(SpringBootSimpleApplication.class, args);
}
}
2).MyServlet2.java类:
package com.example.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;
// 不指定name的情况下,name默认值为类全路径,即com.example.servlet.MyServlet2
@WebServlet(urlPatterns="/st/myservlet2", description="Servlet的说明")
public class Myservlet2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(">>>>>>>>>>doGet2()<<<<<<<<<<<");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(">>>>>>>>>>doPost2()<<<<<<<<<<<");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>大家好,我的名字叫Servlet2</h1>");
out.println("</body>");
out.println("</html>");
}
}
使用 @WebServlet 注解,其中可以设置一些属性。
3.访问结果
4.DispatcherServlet默认拦截
DispatcherServlet 默认拦截“/”,MyServlet 拦截“/st/*”,MyServlet2 拦截“/st/myservlet”,那么在我们访问 http://localhost:8080/st/myservlet2 的时候系统会怎么处理呢?如果访问 http://localhost:8080/st/abc的时候又是什么结果呢?其结果是“匹配的优先级是从精确到模糊,复合条件的Servlet并不会都执行”。
既然系统DispatcherServlet 默认拦截“/”,那么我们是否能做修改呢,答案是肯定的,我们在SpringBootSampleApplication中添加代码:
/**
* 修改DispatcherServlet默认配置
*/
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.getUrlMappings().clear();
registration.addUrlMappings("*.do");
registration.addUrlMappings("*.json");
return registration;
}
可以通过注入DispatcherServlet 然后用ServletRegistrationBean包裹一层 动态的加上一些初始参数。
来源:http://www.cnblogs.com/web424/p/6755950.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#接口interface用法实例
C#如何通过匿名类直接使用访问JSON数据详解
![](https://img.aspxhome.com/file/2023/7/67057_0s.jpg)
Spring Boot缓存实战 EhCache示例
![](https://img.aspxhome.com/file/2023/3/58213_0s.png)
Spring @CrossOrigin 注解原理实现
![](https://img.aspxhome.com/file/2023/1/71851_0s.png)
实例分析java开启线程的方法
![](https://img.aspxhome.com/file/2023/6/104046_0s.png)
Java Integer如何获取第一位和最后一位,并截取
springboot+jersey+tomcat实现跨域方式上传文件到服务器的方式
![](https://img.aspxhome.com/file/2023/9/66679_0s.jpg)
老生常谈java中cookie的使用
monkeyrunner之电脑安装驱动(5)
![](https://img.aspxhome.com/file/2023/4/87334_0s.png)
C#基于HttpWebRequest实现发送HTTP请求的方法分析
Java常用类之System类的使用指南
![](https://img.aspxhome.com/file/2023/0/101770_0s.png)
Spring bean的实例化和IOC依赖注入详解
解决Springboot get请求是参数过长的情况
Android基于OpenCV实现图像修复
![](https://img.aspxhome.com/file/2023/8/137868_0s.png)
Android使用IntentService进行apk更新示例代码
![](https://img.aspxhome.com/file/2023/4/99804_0s.gif)
Quarkus中ConfigSourceInterceptor的加密配置实现
Android Rxjava3 使用场景详解
![](https://img.aspxhome.com/file/2023/3/84723_0s.jpg)
SpringBoot实现Mysql使用MD5进行密码加密的示例
![](https://img.aspxhome.com/file/2023/6/62156_0s.png)
Android实现微信朋友圈发本地视频功能
![](https://img.aspxhome.com/file/2023/6/138966_0s.png)
Opencv基于CamShift算法实现目标跟踪
![](https://img.aspxhome.com/file/2023/2/121672_0s.jpg)