JavaWeb三大组件
:Servlet、Filter、Listener
1.Filter (filter)
1.1. The concept
Web中的过滤器
: When the resource access server, the filter can intercept the request to complete some special features
1.2. Role filter
一般用于完成通用的操作
Such as login authentication, unified coding process, sensitive character filtering
1.3. Filter Quick Start
step:
- 1.
定义一个类,实现接口Filter
A new or directly the Filter
the Filter can modify the template, and a method similar modifications Servlet template:
- 2.
覆写方法
- 3.
配置拦截路径
- 1.
注解的配置
- 2.
web.xml配置
Quick start code is as follows:
package xpu.edu.web.Filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@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() {
}
}
1.4. Filter details
1.4.1. Filter execution flow
执行过滤器
执行放行后的资源
回来执行过滤器放行代码下面的代码
package xpu.edu.web.Filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
public class FilterDemo2 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//对request对象请求参数增强
System.out.println("FilterDemo2执行了");
//放行
chain.doFilter(req, resp);
//对response对象的响应消息增强
System.out.println("FilterDemo2回来了");
}
public void init(FilterConfig config) throws ServletException {
}
}
1.4.2 Lifecycle filter method
init
: After the server is started, it will create a Filter object, and then call the init method is executed only once, usually used to load resourcesdoFilter
: Every request will be executed when intercepted resources, perform multipledestroy
: After the server is shut down, Filter object is destroyed, if the server is shut down properly, it will perform the destroy method is performed only once, typically used for releasing resources
1.4.3. Filter Configuration Explanation
拦截路径的配置
- 1. Specific resource path: such as "/index.jsp", when the only access index.jsp resources, the filter will be executed
- 2. Directory interception: such as "/ user / *", all resource filters will be executed under the user access
- 3. extension interception: such as "* .jsp", resource access all jsp suffix filters will be executed
- 4. intercept all the resources: "/ *", the filter will have access to all the resources to perform
拦截方式的配置
- Interception way: how resources are being accessed, such as the browser to request access to or forwarding
- Interception way configuration
1. Notes Configuration: dispatcherTypes property, there are five values (actually enumeration):
- REQUEST: default browser directly request resources
- FORWARD: Forwarding access to resources
- INCLUDE: includes access to resources
- ERROR: error Jump Resources
- ASYNC: asynchronous access resources
- Configuring multiple values:
2.web.xml
Settings <dispatcher> </ dispatcher> tag to
1.4.4. Filter chain (a plurality of filter)
If there are two filters, filter 1 back to the front of the filter 2, they are 执行顺序
as follows:
- Filter 1
- Filter 2
- Resources to implement
- Filter 2
- Filter 1
There are two filters, know how to filter 1 or filter 2 first? Has the problem:和配置有关
- 1. Configuration Annotations: comparison string comparison rules in accordance with the class name, the first value is smaller. Such as: AFilter and BFilter, AFilter first execution
- 2.web.xml configuration: <filter-mapping> who defined above whoever
2.Listener (listeners)
2.1. The concept
Listener: Web的三大组件之一
, sensing mechanism of events
- Event: One thing
- Event Source: Local happenings
- Listener: An object
- Register listeners: event, event source, the listener bound, when an event occurs on the event source, the listener code execution
2.2.ServletContextListener
ServletContextListener: creating and destroying listening ServletContext object, which has two methods:
- void contextDestroyed (ServletContextEvent sce): This method is called when the object is destroyed ServletContext
- void contextInitialized (ServletContextEvent sce): This method is called after ServletContext object is created
step:
- 1. The definition of a class that implements the interface ServletContextListener
- 2. The method override
- 3. Configuration
- web.xml
- annotation