当前位置:网站首页>web三大组件(Servlet,Filter,Listener)
web三大组件(Servlet,Filter,Listener)
2022-04-23 10:54:00 【yueseck】
目录
一、Servlet
二、Filter
1.概念
* 生活中的过滤器:净水器,空气净化器,土匪、
* web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能。
* 过滤器的作用:
* 一般用于完成通用的操作。如:登录验证、统一编码处理、敏感字符过滤...
2.步骤
1. 定义一个类,实现接口Filter
2. 复写方法
3. 配置拦截路径
1. web.xml
2. 注解
3.代码
@WebFilter("/*")//访问所有资源之前,都会执行该过滤器
public class FilterDemo1 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filterDemo1被执行了....");
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
4.过滤器细节
4.1web.xml配置
<filter>
<filter-name>demo1</filter-name>
<filter-class>cn.itcast.web.filter.FilterDemo1</filter-class>
</filter>
<filter-mapping>
<filter-name>demo1</filter-name>
<!-- 拦截路径 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
4.2过滤器执行流程
1. 执行过滤器
2. 执行放行后的资源
3. 回来执行过滤器放行代码下边的代码
4.3过滤器配置详解
* 拦截路径配置:
1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行
2. 拦截目录: /user/* 访问/user下的所有资源时,过滤器都会被执行
3. 后缀名拦截: *.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行
4. 拦截所有资源:/* 访问所有资源时,过滤器都会被执行
* 拦截方式配置:资源被访问的方式
* 注解配置:
* 设置dispatcherTypes属性
1. REQUEST:默认值。浏览器直接请求资源
2. FORWARD:转发访问资源
3. INCLUDE:包含访问资源
4. ERROR:错误跳转资源
5. ASYNC:异步访问资源
* web.xml配置
* 设置<dispatcher></dispatcher>标签即可
5.过滤器链
* 执行顺序:如果有两个过滤器:过滤器1和过滤器2
1. 过滤器1
2. 过滤器2
3. 资源执行
4. 过滤器2
5. 过滤器1
* 过滤器先后顺序问题:
1. 注解配置:按照类名的字符串比较规则比较,值小的先执行
* 如: AFilter 和 BFilter,AFilter就先执行了。
2. web.xml配置: <filter-mapping>谁定义在上边,谁先执行
三、Listener
* 概念:web的三大组件之一。
* 事件监听机制
* 事件 :一件事情
* 事件源 :事件发生的地方
* 监听器 :一个对象
* 注册监听:将事件、事件源、监听器绑定在一起。 当事件源上发生某个事件后,执行监听器代码
* ServletContextListener:监听ServletContext对象的创建和销毁
* 方法:
* void contextDestroyed(ServletContextEvent sce) :ServletContext对象被销毁之前会调用该方法
* void contextInitialized(ServletContextEvent sce) :ServletContext对象创建后会调用该方法
* 步骤:
1. 定义一个类,实现ServletContextListener接口
2. 复写方法
3. 配置
1. web.xml
<listener>
<listener-class>cn.itcast.web.listener.ContextLoaderListener</listener-class>
* 指定初始化参数<context-param>
2. 注解:
* @WebListener
版权声明
本文为[yueseck]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yueseck/article/details/124327845
边栏推荐
- A diary of dishes | 238 Product of arrays other than itself
- Comparison and practice of prototype design of knowledge service app
- GO接口使用
- SSH uses private key to connect to server without key
- VIM usage
- CentOS/Linux安装MySQL
- 部署jar包
- Windows installs redis and sets the redis service to start automatically
- Leetcode22: bracket generation
- Deploy jar package
猜你喜欢

景联文科技—专业数据标注公司和智能数据标注平台
MapReduce core and foundation demo

【leetcode】199. Right view of binary tree

SQL Server 游标循环表数据

How to quickly download vscode

Visualization Road (10) detailed explanation of segmentation canvas function

Ueditor -- limitation of 4m size of image upload component

精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
Detailed explanation of MapReduce calculation process

Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
随机推荐
Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
Contact between domain name and IP address
【leetcode】199.二叉树的右视图
Strongest date regular expression
全栈交叉编译X86完成过程经验分享
Latex usage
MBA-day5数学-应用题-工程问题
Visual common drawing (IV) histogram
Kaggle - real battle of house price prediction
Comparison and practice of prototype design of knowledge service app
Solution architect's small bag - 5 types of architecture diagrams
【leetcode】107.二叉树的层序遍历II
ID number verification system based on visual structure - Raspberry implementation
Hikvision face to face summary
How to Ping Baidu development board
高价买来的课程,公开了!phper资料分享
景联文科技—专业数据标注公司和智能数据标注平台
MapReduce core and foundation demo
Visual common drawing (III) area map
Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"