当前位置:网站首页>Filter & listener (XIV)
Filter & listener (XIV)
2022-06-25 04:47:00 【hercu1iz】
Filter: 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. Quick start :
1. step :
1. Define a class , Implementation interface Filter
2. Replication method
3. Configure interception path
1. web.xml
2. annotation
2. 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() {
}
}
3. Filter details :
1. web.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>
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
3. Filter life cycle approach
1. init: After the server starts , Will create Filter object , And then call init Method . Only once . Used to load resources
2. doFilter: Every time a request is intercepted , Will execute . Execute many times
3. destroy: After the server is shut down ,Filter Object destroyed . If the server is shut down normally , Will perform destroy Method . Only once . Used to free resources
4. 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 ( Configure multiple filters )
* 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
4. Case study :
1. Case study 1_ validate logon
* demand :
1. visit day17_case Resources for cases . Verify that they are logged in
2. If you log in , Then direct release .
3. If you are not logged in , Then go to the login page , Tips " You are not signed in , Please log in first ".
2. Case study 2_ Sensitive word filtering
* demand :
1. Yes day17_case The data entered in the case is filtered by sensitive words
2. Sensitive vocabulary reference 《 Sensitive words .txt》
3. If it's a sensitive word , Replace with ***
* analysis :
1. Yes request Object enhancement . Enhanced methods for obtaining parameters
2. release . Passing proxy objects
* Enhance the functionality of objects :
* Design patterns : Some common ways to solve fixed problems
1. Decoration mode
2. The proxy pattern
* Concept :
1. Real object : The proxied object
2. Proxy object :
3. The proxy pattern : The proxy object represents the real object , To enhance the function of real objects
* Realization way :
1. Static proxy : There is a class file that describes the proxy pattern
2. A dynamic proxy : Form a proxy class in memory
* Implementation steps :
1. Proxy objects and real objects implement the same interface
2. Proxy object = Proxy.newProxyInstance();
3. Use proxy objects to call methods .
4. Enhancement method
* Enhancements :
1. Enhanced parameter list
2. Enhanced return value type
3. Enhance method body execution logic
Listener: Monitor
* 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>
</listener>
* Specify initialization parameters <context-param>
2. annotation :
* @WebListener
边栏推荐
- leetcode1221. 分割平衡字符串
- The solution of wechat applet switchtab unable to take parameters
- Part I Verilog quick start
- JS' sort() function
- Record the problem of C # print size once
- JS arguments
- Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg
- dotnet-exec 0.4.0 released
- Data view for gbase 8s
- CTF_ Web: Learn flask template injection (SSTI) from 0
猜你喜欢

高效的NoSQL数据库服务Amozon DynamoDB体验分享

Vscade setting clang format

哪个编程语言实现hello world最烦琐?

Why does the SQL statement hit the index faster than it does not?

Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影

【FLink】access closed classloader classloader.check-leaked-classloader

Paper notes: multi label learning ESMC (I don't understand it, but I haven't written it yet, so I'll put it here for a place temporarily)

File upload vulnerability shooting range upload labs learning (pass1-pass5)

执行SQL响应比较慢,你有哪些排查思路?

Kotlin Compose 监听软键盘 点击enter提交事件
随机推荐
「 每日一练,快乐水题 」1108. IP 地址无效化
大话云原生数据库中的存算分离
Vscade setting clang format
Why PHP is not safe
Record of the 25th week
【Keil】ADuCM4050官方库的GPIO输出宏定义
为什么SQL语句命中索引比不命中索引要快?
Huawei Hongmeng development lesson 4
Trigger for gbase 8s
Machine learning deep learning -- Vectorization
WPF 使用 MAUI 的自绘制逻辑
机器学习深度学习——向量化
Construction scheme of distributed websocket
Record small knowledge points
哪个编程语言实现hello world最烦琐?
重磅直播 | 相移法+多频外差之数学原理推导+实现
魔法猪系统重装大师怎么使用
MySQL concept and operation (III)
成功解决:selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from
Cascading deletion of gbase 8s