【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
**开源地址:https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB **
extends javax.servlet.Filter and puts the @WebFilter annotation with filterName
继承javax.servlet.Filter并将@WebFilter注释与filterName放在一起
and urlPatterns parameters, which define the filter name and the URLs to filter, before
和urlPatterns参数,它们定义了过滤器名称和要过滤的url
the definition of class. The following is a snippet of code that demonstrates how to do that
class的定义。下面是一段代码,演示了如何做到这一点
@WebFilter(filterName = “LogAccess”, urlPatterns = “/*”)
public class LogAccessFilter implements javax.servlet.Filter{
…
}
Note that the servlet filter uses the chain of responsibility pattern to walk throughout the
注意,servlet过滤器使用责任链模式遍历整个
filters (objects that are servlet filter). The following is a snippet of code that uses a chain of
过滤器(servlet过滤器对象)。以下是使用
responsibility pattern:
责任模式:
chain.doFilter(req, resp);
In the preceding line of code, we established the filter name as LogAccess through
在前面的代码行中,我们将过滤器名称设置为LogAccess through
the filterName parameter. This will filter all requests, because
filterName参数。这将过滤所有请求,因为
the urlPatterns parameter has the “/_” value. If we filter according to servlet name, we
urlPatterns参数具有“/_”值。如果我们根据servlet名称进行过滤,那么
need to use the following annotation:
需要使用以下注释:
//Servlet1 and Servlet2 are the servlets to filter.
@WebFilter(filterName = “LogAccess”, servletNames =
{“servlet1”,“servlet2”})
The doFilter method is responsible for pre-processing and post-processing and
doFilter方法负责前处理和后处理以及
establishes when to follow the request to the next filter or servlet (main logic). To follow the
确定何时跟踪请求到下一个过滤器或servlet(主逻辑)。跟随
request to the next filter or servlet, the following code needs be executed:
请求下一个筛选器或servlet时,需要执行以下代码:
//Following to next filter or servlet.
chain.doFilter(req, resp);
When the preceding code is executed, the current filter executes only the next line when the
当执行前面的代码时,当前筛选器只执行下一行
other filters and servlets finish their processing.
其他过滤器和servlet完成它们的处理。
Implementing LogBrowserFilter
The implementation of LogBrowserFilter is as follows:
package com.gary.book.chapter01;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@WebFilter(filterName = “LogBrowser”, urlPatterns = “/*”)
public class LogBrowserFilter implements Filter {
private static Logger logger = LogManager.getLogger(LogBrowser.class);
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws ServletException, IOException {
//Get userAgent that contain browse informations.
String userAgent = ((HttpServletRequest) req).getHeader(“UserAgent”);
//Get IP of Client that sent a resquest.
String ip = ((HttpServletRequest) req).getRemoteAddr();
//Logging the informations of IP and Browser.
logger.info("IP: " + ip + " Browser info: " + userAgent);
//Following to the next filter. If none next filter exist, follow to main logic.
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
In the preceding filter, we get the client IP and browser information and log them. The
在前面的过滤器中,我们获取客户端IP和浏览器信息并记录它们。这个
LogBrowserFilter operation is similar to that of LogAccessFilter.
LogBrowserFilter的操作与LogAccessFilter的操作类似。
To define the order of filter execution, we need to configure the web.xml and add the filter
要定义过滤器执行的顺序,我们需要配置web.xml文件再加上过滤器
mapping information. Here, we can see web.xml with its configuration:
映射信息。在这里,我们可以看到web.xml文件其配置:
<web-app version=“3.1”
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_3_1.xsd">
LogBrowser
com.rhuan.filter.LogBrowserFilter
LogAccess
com.rhuan.filter.LogAccessFilter
The configurations defined in web.xml override the annotation configurations. Thus, if we
中定义的配置web.xml文件重写注释配置。因此,如果我们
put the urlPattern configuration on web.xml, then the configuration considered
启用urlPattern配置web.xml文件,然后考虑配置
is web.xml’s configuration. We don’t put the filter mapping information on web.xml
是web.xml文件的配置。我们不把过滤器映射信息放在web.xml文件
because this is already on the annotation configuration in the code.
因为这已经在代码中的注释配置上了。
The web.xml configuration defines the order—LogBrowserFilter will be called first,
这个web.xml文件配置定义将首先调用LogBrowserFilter的顺序,
followed by LogAccessFilter, and then the main logic (servlet).
接着是LogAccessFilter,然后是主逻辑(servlet)。
Deciding filter mapping
定义滤波器映射
Defining the mapping method is crucial to implementing the intercepting filter pattern.
定义映射方法是实现拦截过滤模式的关键。
This is because a bad method for mapping can impact the project directly and cause
这是因为不好的映射方法会直接影响项目并导致
rework. We have two filter mapping types—UrlPattern and servlet name.
返工。我们有两种过滤器映射类型UrlPattern和servlet name。
The use of UrlPatterns is indicated when we want to filter the HTTP Requests to nonspecific resources or files, but we also want to filter various unknown resources. Here are
当我们想过滤对非特定资源或文件的HTTP请求时,使用UrlPatterns是有指示的,但是我们也希望过滤各种未知资源。给你
some examples of this:
这方面的一些例子:
_.jsp: This filters all requests to JSP pages. If one JSP page is added to the
_.jsp:它过滤对jsp页面的所有请求。如果一个JSP页面被添加到
server, then the filter will filter the new JSP page without making any
服务器,则过滤器将过滤新的JSP页面,而不进行任何
modifications.