1 回答
TA贡献35条经验 获得超11个赞
首先,在web.xml中配置如下信息:
<filter>
<filter-name>log //filter 名字</filter-name>
<filter-class>LogFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>log</filter-name>
<servletname>servletname</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>servletname</servletname>
<servletclass>servletclass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletname</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
然后,编写LogFilter 代码:
public class LogFilter implements Filter {
FilterConfig config;
public void setFilterConfig(FilterConfig config) {
this.config = config;
}
public FilterConfig getFilterConfig() {
return config;
}
public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain) {
ServletContext context = getFilterConfig().getServletContext();
context.log("请求开始");
chain.doFilter(req, res);
long aft = System.currentTimeMillis();
context.log("请求结束");}
}
当每次请求一个request时,先到LogFilter中去并调用doFilter()方法,开始打印请求开始和请求结束日志。
添加回答
举报