当前位置:网站首页>Three web components (servlet, filter, listener)
Three web components (servlet, filter, listener)
2022-04-23 11:03:00 【yueseck】
Catalog
4.3 Filter configuration details
One 、Servlet
Click here to introduce the past servlet
Two 、Filter
1. Concept
* Filters in life : Water purifier , air cleaner , bandit 、
* web Filter in : When accessing the server's resources , Filters can intercept requests , Complete some special functions .
* The function of the filter :
* Generally used to complete general operations . Such as : validate logon 、 Unified coding 、 Sensitive character filtering ...
2. step
1. Define a class , Implementation interface Filter
2. Replication method
3. Configure interception path
1. web.xml
2. annotation
3. Code
@WebFilter("/*")// Before accessing all resources , Will execute the filter
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 Carried out ....");
// release
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
4. Filter details
4.1web.xml To configure
<filter>
<filter-name>demo1</filter-name>
<filter-class>cn.itcast.web.filter.FilterDemo1</filter-class>
</filter>
<filter-mapping>
<filter-name>demo1</filter-name>
<!-- Intercept path -->
<url-pattern>/*</url-pattern>
</filter-mapping>
4.2 Filter execution process
1. Perform filter
2. Resources after implementation of release
3. Come back and execute the code below the filter release code
4.3 Filter configuration details
* Intercept path configuration :
1. Specific resource paths : /index.jsp Only visit index.jsp Resource time , The filter will be executed
2. Intercept Directory : /user/* visit /user All resources under , Filters are executed
3. Suffix blocking : *.jsp Access all suffixes named jsp Resource time , Filters are executed
4. Intercept all resources :/* When accessing all resources , Filters are executed
* Interception mode configuration : How resources are accessed
* Annotation configuration :
* Set up dispatcherTypes attribute
1. REQUEST: The default value is . Browser requests resources directly
2. FORWARD: Forward access to resources
3. INCLUDE: Contains access to resources
4. ERROR: Wrong jump resource
5. ASYNC: Asynchronous access to resources
* web.xml To configure
* Set up <dispatcher></dispatcher> Labels can be
5. Filter chain
* Execution order : If there are two filters : filter 1 And filters 2
1. filter 1
2. filter 2
3. Resource execution
4. filter 2
5. filter 1
* The filter sequence problem :
1. Annotation configuration : Compare according to the string comparison rules of class names , If it's worth less, execute it first
* Such as : AFilter and BFilter,AFilter Just do it first .
2. web.xml To configure : <filter-mapping> Who is defined above , Who will execute first
3、 ... and 、Listener
* Concept :web One of the three components of .
* Event monitoring mechanism
* event : One more thing
* Event source : Where it happened
* Monitor : An object
* Register to listen : Put the event 、 Event source 、 Listeners are bound together . When an event occurs on the event source , Execute listener code
* ServletContextListener: monitor ServletContext Object creation and destruction
* Method :
* void contextDestroyed(ServletContextEvent sce) :ServletContext The method is called before the object is destroyed
* void contextInitialized(ServletContextEvent sce) :ServletContext The method is called after the object is created
* step :
1. Define a class , Realization ServletContextListener Interface
2. Replication method
3. To configure
1. web.xml
<listener>
<listener-class>cn.itcast.web.listener.ContextLoaderListener</listener-class>
* Specify initialization parameters <context-param>
2. annotation :
* @WebListener
版权声明
本文为[yueseck]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231053377300.html
边栏推荐
- 精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
- Cygwin 中的 rename 用法
- Visual Road (XII) detailed explanation of collection class
- Learning website materials
- MBA - day5 mathématiques - Questions d'application - Questions d'ingénierie
- Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities
- How to bind a process to a specified CPU
- Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
- MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
- Strongest date regular expression
猜你喜欢

关于JUC三大常用辅助类

Use of SVN:

精彩回顾|「源」来如此 第六期 - 开源经济与产业投资

Solutions to common problems in visualization (IX) background color

GO接口使用

Detailed explanation of typora Grammar (I)

Typora operation skill description (I) md

Notes on concurrent programming of vegetables (V) thread safety and lock solution

Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)

Visual solutions to common problems (VIII) mathematical formulas
随机推荐
精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
Visual Road (XII) detailed explanation of collection class
Understand the key points of complement
HuggingFace
Solutions to common problems in visualization (IX) background color
学习 Go 语言 0x03:理解变量之间的依赖以及初始化顺序
Full stack cross compilation x86 completion process experience sharing
ID number verification system based on visual structure - Raspberry implementation
软件测试人员,如何优秀的提Bug?
学习 Go 语言 0x01:从官网开始
Full stack cross compilation x86 completion process experience sharing
使用zerotier让异地设备组局域网
Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
Facing the global market, platefarm today logs in to four major global platforms such as Huobi
MySQL面试题讲解之如何设置Hash索引
Learning website materials
【leetcode】107. Sequence traversal of binary tree II
SWAT—Samba WEB管理工具介绍
STM32接电机驱动,杜邦线供电,然后反烧问题
Cygwin 中的 rename 用法