Filter (3)【日志记录与异常捕捉】

一:

记录日志也是Filter擅长的工作之一,在request之前和之后都可以记录日志。该日志记录Filter使用 apache的日志工具记录日志,记录客户的IP地址,访问的URL以及消耗的时间。

package com.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class LogFilter implements Filter { private Log log = LogFactory.getLog(this.getClass()); private String filterName; @Override public void init(FilterConfig config) throws ServletException { // 获取 Filter 的name,设置在 web-xml 中。 filterName = config.getFilterName(); log.info("自动获取 Filter :" + filterName); } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; long startTime = System.currentTimeMillis(); String requestURI = request.getRequestURI(); // request.getQueryString() 获取查询字符串 比如客户端发送. http://localhost/test.do?a=b&c=d&e=f // 通过request.getQueryString()得到的是 a=b&c=d&e=f .. requestURI = request.getQueryString() == null ? requestURI : (requestURI + "?" + request.getQueryString()); chain.doFilter(request, response); long endTime = System.currentTimeMillis(); log.info(request.getRemoteAddr() + "访问了" + requestURI + ",总共用时" + (endTime - startTime) + "毫秒"); } @Override public void destroy() { // TODO Auto-generated method stub } }

别忘了 : 配置文件,log4j.Properties

log4j.rootLogger=INFO, A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%l]-[%p] %m%n


二:异常捕获


package com.filter; import java.io.IOException; import javax.security.auth.login.AccountException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import com.filter.exception.BusinessException; public class ExceptionHandlerFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { chain.doFilter(request, response); } catch (Exception e) { Throwable rootCause = e;// 根异常 while( rootCause.getCause() != null){ // 循环 直到寻找到根异常 为止。 rootCause = rootCause.getCause(); } String message = rootCause.getMessage(); message = message == null ? "异常:" + rootCause.getClass().getName() : message; request.setAttribute("message", message); request.setAttribute("e", e); if(rootCause instanceof AccountException) { request.getRequestDispatcher("/accountException.jsp").forward(request, response); }else if(rootCause instanceof BusinessException) { request.getRequestDispatcher("/businessException.jsp").forward(request, response); }else { request.getRequestDispatcher("/exception.jsp").forward(request, response); } } } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
<%@ page language="java" contentType="text/html; charset=UTF-8" %> <%@ page import="com.filter.exception.AccountException" %> <%@ page import="com.filter.exception.BusinessException" %> <% String action = request.getParameter("action"); if("businessException".equals(action)){ throw new BusinessException("业务操作失败 。 "); }else if("accountException".equals(action)){ throw new AccountException("您需要登陆后再进行此项操作. "); }else if("exception".equals(action)){ Integer.parseInt(""); } %> Insert title here test businessException
test accountException
test exception
<%@ page language="java" contentType="text/html; charset=UTF-8" %> Insert title here
${ message }返回

<%@ page language="java" contentType="text/html; charset=UTF-8"%> Insert title here
${ message }
账号
密码



你可能感兴趣的:(Filter (3)【日志记录与异常捕捉】)