日期:2014-05-20 浏览次数:20836 次
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 javax.servlet.http.HttpSession; public class ExceptionFilter implements Filter { protected FilterConfig filterConfig = null; private static final String PAGE_TO_FORWARD = "errorNew.jsp"; // ---------------------- Public Methods public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; HttpServletRequest request = (HttpServletRequest) req; HttpSession session = request.getSession(); Exception e=null; long millsecds=0; millsecds=System.currentTimeMillis(); try { chain.doFilter(req, res); }catch(NullPointerException ee){ e=ee; session.setAttribute("msg", "空指针异常!异常编号是:"+millsecds); } catch (Exception ee) { e=ee; session.setAttribute("msg", "异常!异常编号是:"+millsecds); } //把异常信息保存到 saveExceptionToDataBase(e,millsecds); session.setAttribute("e", e); redirect(PAGE_TO_FORWARD, request, response); } public void init(FilterConfig filterConfig) { } public void destroy() { } // --------------------- Private Methods private void redirect(String page, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect(request.getContextPath() + "/" + page); } private void saveExceptionToDataBase(Exception e,long millsecds){ //把异常保存到db中 } }