官术网_书友最值得收藏!

Implementing LogAccessFilter

Here, we have the implementation of LogAccessFilter:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

@WebFilter(filterName = "LogAccess", urlPatterns = "/*")
public class LogAccessFilter implements javax.servlet.Filter {

private static Logger logger = LogManager.getLogger(LogAccess.class);

public void destroy() {
}

public void doFilter(javax.servlet.ServletRequest req,
javax.servlet.ServletResponse resp, javax.servlet.FilterChain
chain) throws javax.servlet.ServletException, IOException {

//Gets the initial date of request.
Date dateInitRequest = new Date();

//Get IP of Client that sent a resquest.
String ip = ((HttpServletRequest)req).getRemoteAddr();

//Following to next filter. If none next filter exist, follows
//for main logic.
chain.doFilter(req, resp);

//Gets the end date of request.
Date dateEndRequest = new Date();

//Logging the informations of IP and access time.
logger.info("IP: "+ip +" Access time : "
+ Long.toString(dateEndRequest.getTime()
- dateInitRequest.getTime())
+ " ms");

}

public void init(javax.servlet.FilterConfig config) throws
javax.servlet.ServletException {

}

}

As we can see in the code, to create one servlet filter, we need to create a class that extends javax.servlet.Filter and puts the @WebFilter annotation with filterName and urlPatterns parameters, which define the filter name and the URLs to filter, before the definition of class. The following is a snippet of code that demonstrates how to do that:

@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 filters (objects that are servlet filter). The following is a snippet of code that uses a chain of responsibility pattern:

chain.doFilter(req, resp);

In the preceding line of code, we established the filter name as LogAccess through the filterName parameter. This will filter all requests, because the urlPatterns parameter has the "/*" value. If we filter according to servlet name, we 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 establishes when to follow the request to the next filter or servlet (main logic). To follow the request to the next filter or servlet, the following code needs be executed:

//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.

主站蜘蛛池模板: 秦安县| 自贡市| 五原县| 梓潼县| 南召县| 夏津县| 云梦县| 上饶县| 芦溪县| 新绛县| 黄平县| 广河县| 蒙阴县| 崇仁县| 毕节市| 万山特区| 朝阳县| 九龙县| 郎溪县| 内乡县| 闸北区| 寻甸| 五华县| 陕西省| 华容县| 龙江县| 根河市| 衡水市| 恭城| 抚松县| 棋牌| 许昌市| 淮安市| 通许县| 基隆市| 广州市| 阿克苏市| 天水市| 铜川市| 金溪县| 玉林市|