javaWeb filter-filter-filter execution process
filter-filter execution process
- When a student enters the campus, he will meet the security guard first, and if the student leaves the campus, he will also encounter the security guard
- Filter execution process
- The tomcat engine converts the http request into a request object, and the http response into a response object
- If the address meets the conditions, the request and resposne will be passed to the doFilter method
- Pass it to the chain.doFilter(request,response) method
- Then pass to the doGet or doPost method, when the servlet has completed three things
- After doFilter executes the remaining code, the response returns to the browser
- Set the code position for encoding
- Set the request encoding before doFilter, and set the response encoding before
##filter-The execution order of filters
- If there are two filters that filter requests, what is the order of execution?
First Filter1, then Filter2, and finally Servlet, the opposite is the case when the response is returned
-
What setting determines the execution order of the Filter?
-
De-configuration
When there are multiple filters, they are executed in the order of the first letter of the filter classAFilter first, then BFilter
-
web.xml configuration
when a plurality of filters, in accordance web.xml is<filter-mapping></filter-mapping>
arranged successively in the order execution
##Code Case
AFilter:
package com.lbl.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/s2")
public class AFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("第1个过滤器收到请求");
chain.doFilter(req, resp);
System.out.println("第1个过滤器收到响应");
}
public void init(FilterConfig config) throws ServletException {
}
}
BFilter
package com.lbl.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/s2")
public class BFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("第2个过滤器收到请求");
chain.doFilter(req, resp);
System.out.println("第2个过滤器收到响应");
}
public void init(FilterConfig config) throws ServletException {
}
}
Demo02Servlet
package com.lbl.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/s2")
public class Demo02Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("servlet执行了");
}
}
running result:
Comment @WebFilter, change to xml configuration
OneFilter
package com.lbl.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//@WebFilter("/s3")
public class OneFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("第one个过滤器收到请求");
chain.doFilter(req, resp);
System.out.println("第one个过滤器收到响应");
}
public void init(FilterConfig config) throws ServletException {
}
}
TwoFilter
package com.lbl.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//@WebFilter("/s3")
public class TwoFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("第two个过滤器收到请求");
chain.doFilter(req, resp);
System.out.println("第two个过滤器收到响应");
}
public void init(FilterConfig config) throws ServletException {
}
}
Demo03Servlet
package com.lbl.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//@WebServlet("/s3")
public class Demo03Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Demo03Servlet....servlet执行了");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Demo3Servlet</servlet-name>
<servlet-class>com.lbl.servlet.Demo03Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo3Servlet</servlet-name>
<url-pattern>/s3</url-pattern>
</servlet-mapping>
<filter>
<filter-name>TwoFilter</filter-name>
<filter-class>com.lbl.filter.TwoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>TwoFilter</filter-name>
<url-pattern>/s3</url-pattern>
</filter-mapping>
<filter>
<filter-name>OneFilter</filter-name>
<filter-class>com.lbl.filter.OneFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OneFilter</filter-name>
<url-pattern>/s3</url-pattern>
</filter-mapping>
</web-app>
running result:
filter-filter life cycle (understand)
There are 3 methods for filters:
- init method: the filter object is created when the server starts
- doFilter method: Whenever the path of a request is the configured path that satisfies the filter, the doFilter method of the filter will be executed once
- destroy method: the filter is destroyed when the server is shut down