Spring Boot 2 使用Servlet、Listener和Filter配置
开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
新建一个名称为demo的Spring Boot项目。
一、使用Servlet配置
1、修改启动类 DemoApplication.java代码,加入注解ServletComponentScan,它用于扫描Servlet组件,包括使用@WebServlet、
@WebFilter和@WebListener进行修饰的类。
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2、新建一个类 MyServlet.java,继承HttpServlet并且加入注解 @WebServlet
package com.example.demo; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet(value="/servlet")
public class MyServlet extends HttpServlet {
public MyServlet(){
System.out.println("servlet类");
}
protected void service(HttpServletRequest arg0, HttpServletResponse arg1){
System.out.println("servlet方法");
}
}
在浏览器中访问http://localhost:8080/servlet,可看到IDEA控制台输出
servlet类
servlet方法
二、使用Listener配置
1、启动类 DemoApplication.cs 代码在使用Servlet配置上已经加入注解ServletComponentScan,在此保持不变。
2、新建一个类 MyServlet.java
package com.example.demo; import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener; @WebListener
public class MyListener implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent){
System.out.println("请求创建");
}
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent){
System.out.println("请求销毁");
}
}
在浏览器中访问一个接口,如上步http://localhost:8080/servlet,可看到IDEA控制台输出:
请求创建
请求销毁
附,常用的监听器接口:
1.ServletContextListener -- 监听servletContext对象的创建以及销毁
1.1 contextInitialized(ServletContextEvent arg0) -- 创建时执行
1.2 contextDestroyed(ServletContextEvent arg0) -- 销毁时执行
2.HttpSessionListener -- 监听session对象的创建以及销毁
2.2 sessionCreated(HttpSessionEvent se) -- 创建时执行
2.2 sessionDestroyed(HttpSessionEvent se) -- 销毁时执行
3.ServletRequestListener -- 监听request对象的创建以及销毁
3.1 requestInitialized(ServletRequestEvent sre) -- 创建时执行
3.2 requestDestroyed(ServletRequestEvent sre) -- 销毁时执行
4.ServletContextAttributeListener -- 监听servletContext对象中属性的改变
4.1 attributeAdded(ServletContextAttributeEvent event) -- 添加属性时执行
4.2 attributeReplaced(ServletContextAttributeEvent event) -- 修改属性时执行
4.3 attributeRemoved(ServletContextAttributeEvent event) -- 删除属性时执行
5.HttpSessionAttributeListener --监听session对象中属性的改变
5.1 attributeAdded(HttpSessionBindingEvent event) -- 添加属性时执行
5.2 attributeReplaced(HttpSessionBindingEvent event) -- 修改属性时执行
5.3 attributeRemoved(HttpSessionBindingEvent event) -- 删除属性时执行
6.ServletRequestAttributeListener --监听request对象中属性的改变
6.1 attributeAdded(ServletRequestAttributeEvent srae) -- 添加属性时执行
6.2 attributeReplaced(ServletRequestAttributeEvent srae) -- 修改属性时执行
6.3 attributeRemoved(ServletRequestAttributeEvent srae) -- 删除属性时执行
三、使用Filter配置
1、启动类 DemoApplication.cs 代码在使用Servlet配置上已经加入注解ServletComponentScan,在此保持不变。
2、新建一个类 MyFilter.java
package com.example.demo; import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException; //第一个参数为过滤器名字,第二个参数为要拦截的请求地址
@WebFilter(filterName="myFilter", urlPatterns="/*")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter初始化");
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("filter方法");
chain.doFilter(request, response);
} @Override
public void destroy() {
System.out.println("filter销毁");
}
}
在浏览器中访问http://localhost:8080/servlet,可看到IDEA控制台输出:
filter方法
最后,附上项目结构图:
Spring Boot 2 使用Servlet、Listener和Filter配置的更多相关文章
- 从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)
在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...
- Spring boot中使用servlet filter
Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...
- Spring Boot中使用Servlet与Filter
在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...
- (7)Spring Boot web开发 --- servlet容器
文章目录 配置嵌入式 Servlet 容器 注册 三大组件 使用其他 servlet 容器 使用外置的 `Servlet` 容器 配置嵌入式 Servlet 容器 Spirng Boot 默认使用自带 ...
- Servlet, Listener 、 Filter.
Java Web的三大组件:Servlet, Listener . Filter. 使用Listener监听器:八大监听器: 第一组:用于监听Servlet三个域对象的创建与销毁 1. Servlet ...
- Spring boot中注册Servlet
Spring boot中注册Servlet 如何在spring boot项目中注册Servlet呢? 如何在spring boot项目中注册Servlet呢? 由于没有web.xml,无法直接在xml ...
- Spring boot 默认静态资源路径与手动配置访问路径的方法
这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在application.propertis中配置 ##端口号 ...
- Spring boot项目maven的profile多环境配置不自动替换变量的问题解决
Spring boot项目maven的profile多环境配置不自动替换变量的问题解决 在网上找了好久,配置都很简单,可是我的程序就是不能自动替换变量,最终单独测试,发现原来是引用spring b ...
- spring boot: 中文显示乱码,在applicationContext里面配置
spring boot: 中文显示乱码,在applicationContext里面配置 applicationContext.properties ########################## ...
- Spring Boot 2.4版本前后的分组配置变化及对多环境配置结构的影响
前几天在<Spring Boot 2.4 对多环境配置的支持更改>一文中,给大家讲解了Spring Boot 2.4版本对多环境配置的配置变化.除此之外,还有一些其他配置变化,所以今天我们 ...
随机推荐
- 安装SDK 6.0(二)
2==>安装SDK 6.0 打开安卓Android Studio 出现 Unable to access Android SDK add-on list 点击 Cancal 在点击Cancel ...
- Javassist中文技术文档
本文译自Getting Started with Javassist,如果谬误之处,还请指出. bytecode读写 ClassPool Class loader 自有和定制 Bytecode操控接口 ...
- 微软发布ML.NET 1.0
原文地址:https://devblogs.microsoft.com/dotnet/announcing-ml-net-1-0/ 我们很高兴地宣布今天发布ML.NET 1.0. ML.NET是一个 ...
- linux-发送文件夹rsync -avz salt-发送文件/文件夹
linux下同步文件夹 rsync -avz /local_position/test_dir/ root@192.168.1.165:/target_position # ps:rsync -avz ...
- Android组件体系之Activity启动模式解析
本文主要分析Activity的启动模式及使用场景. 一.Activity启动模式浅析 1.standard 标准模式,系统默认的启动模式.在启动Activity时,系统总是创建一个新的Activity ...
- 使用Docker Compose 部署Nexus后初次登录账号密码不正确,并且在nexus-data下没有admin,password
场景 Ubuntu Server 上使用Docker Compose 部署Nexus(图文教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/ ...
- HTML入门(列表、表单、常用表单控件、浮动框架、iframe、 摘要与细节、度量标签)
一.列表 1.作用:默认显示方式为从上到下的显示数据 2.列表的组成 列表类型和列表项 3.列表的分类:有序列表 无序列表 自定义列表 无序列表语法为ul>li, 语法:ul代表列表,l ...
- ABAP分享一 弹出框函数的简单示例
在开发中经常会使用到弹出框这个功能,在SAP中有很多函数可以实现类似的功能,这里介绍一个比较简单常用的函数 POPUP_TO_CONFIRM 下面是一个实现的简单示例: TABLES sscrfie ...
- 发送RCS 消息摘录相关成功log
//11-25 16:48:09.612102 2175 2726 I BugleDataModel: PendingMessagesProcessor: process from InsertN ...
- 阿里云ECS服务器部署HADOOP集群(六):Flume 安装
本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建. 1 环境介绍 一台阿里云ECS服务器:master 操作系统:CentOS 7.3 Hadoop ...