fileter异常处理

package com.ajun.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 com.ajun.exception.BsException;

public class ExceptionFiler implements Filter {

private String errorPage;//跳转的错误信息页面


public void destroy() {

}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {

HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;

try {
chain.doFilter(req, res);
} catch (Exception e) {
request.setAttribute("exception", e);
request.getRequestDispatcher(errorPage).forward(request, response);
e.printStackTrace();
}
}

public void init(FilterConfig config) throws ServletException {
//读取错误信息提示页面路径
errorPage = config.getInitParameter("errorPage");
if(null==errorPage || "".equals(errorPage)){
throw new RuntimeException("没有配置错误信息跳转页面,请再web.xml中进行配置\n<init-param>\n<param-name>errorPage</param-name>\n<param-value>/error.jsp</param-value>\n </init-param>\n路径可以是你自己设定的任何有效路径页面!!");
//System.out.println("没有配置错误信息跳转页面");
}
}

}

你可能感兴趣的:(File)