项目为给客户提供较好的展示,通常会对异常进行统一处理,除去框架处理外,可以采用servlet处理:
1.在web.xml文件中配置异常处理。
<error-page> <error-code>404</error-code> <location>/common/404.jsp</location> </error-page>
这种方式要为每种异常都一个异常配置,处理比较单一。
2.采用Filter过滤控制统一管理异常。
<filter> <filter-name>ExceptionFilter</filter-name> <filter-class>com.hsit.ExceptionFiler</filter-class> <init-param> <param-name>errorPage</param-name> <param-value>/error.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>ExceptionFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <servlet> <servlet-name>ExServlet</servlet-name> <servlet-class>com.hsit.ExServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ExServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
/**
* 业务异常类
*/
public class BsException extends RuntimeException {
private static final long serialVersionUID = 6744141471703095812L;
//异常代码
private String key;
private Object[] values;//一些其他信息
public BsException() {
super();
}
public BsException(String message, Throwable throwable) {
super(message, throwable);
}
public BsException(String message) {
super(message);
}
public BsException(Throwable throwable) {
super(throwable);
}
public BsException(String message,String key){
super(message);
this.key = key;
}
public BsException(String message,String key,Object value){
super(message);
this.key = key;
this.values = new Object[]{value};
}
public BsException(String message,String key,Object[] values){
super(message);
this.key = key;
this.values = values;
}
public String getKey() {
return key;
}
public Object[] getValues() {
return values;
}
}
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;
/**
* 业务异常过滤器
*/
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 (RuntimeException e) {
if(e instanceof BsException){//如果是你定义的业务异常
request.setAttribute("BsException", 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("没有配置错误信息跳转页面");
}
}
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ExServlet extends HttpServlet {
private static final long serialVersionUID = 2303258011121708614L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
TestService t = new TestService();
t.add();//模拟调用业务层方法,在此方法内抛出异常,此异常会在filter中进行捕获。
}
}
public class TestService {
public void add(){
System.out.println("add () was called!!");
if(true){
throw new BsException("in add() throws exception!!");//抛出异常,根据你的业务逻辑
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>运行时异常处理</title>
</head>
<body>
<form action="/ExceptionHandler/exceptionTest.do">
<input type="submit" value="测试"></input>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>捕获异常</title>
</head>
<body>
</body>
</html>