当前位置:网站首页>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
边栏推荐
- Join() in JSZ
- Web3 DApp用户体验最佳实践
- Introduction to the isolation level of gbase 8s
- OOP stack class template (template +ds)
- 《牛客刷verilog》Part I Verilog快速入门
- The SQL response is slow. What are your troubleshooting ideas?
- PHP encapsulates curl to send get and post request methods, and uses
- Construction scheme of distributed websocket
- Records of ros2/dds/qos/ topics
- 2.0springmvc uses restful
猜你喜欢

Region of Halcon: generation of multiple regions (3)

halcon之区域:多种区域(Region)生成(3)

Gbase 8s overall architecture

为什么TCP握手刚刚好是3次呢?

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

Gbase 8s index R tree

At the age of 30, I began to learn programming by myself. Is it still time for me to have difficulties at home?

Separation of storage and computing in Dahua cloud native database

Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.

Bingbing's learning notes: implementation of circular queue
随机推荐
MySQL concept and operation (III)
Gbase 8s stored procedure execution and deletion
Web3 DAPP user experience best practices
固态硬盘开盘数据恢复的方法
Vscode 设置clang-format
dotnet-exec 0.4.0 released
Upgrade PHP to php7 X (III) failure of wechat payment callback
CTF_ Web: Changan cup-2021 old but a little new & asuka
OpenSea PHP开发包
30岁了开始自学编程,家里比较困难还来得及吗?
JS arguments
小白一键重装官网下载使用方法
Cascading deletion of gbase 8s
Gbase 8s index b+ tree
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
Use of deferred environment variable in gbase 8s
分布式websocket搭建方案
Construction scheme of distributed websocket
Concat() in JS
【Keil】ADuCM4050官方库的GPIO输出宏定义