在服务器端进行异常处理方法

本文介绍了一种在服务器端实现异常处理的方法,包括自定义异常类、验证接口及其实现,并通过Struts框架进行异常捕获与处理。
 
在服务器端进行异常处理:
1.各种类型的异常类:
publicclass ValidateException extends RuntimeException {
 
    public ValidateException() {}
 
    public ValidateException(String msg) {
        super(msg);
    }
 
    public ValidateException(int msgId) {
        super();
 
    }
 
 
}
publicclass CreateException extends RuntimeException {
 
    public CreateException() {}
 
    public CreateException(String msg) {
        super(msg);
    }
 
    public CreateException(Throwable throwable) {
        super(throwable);
    }
 
}
 
2.定义VO中的验证接口及方法:
publicinterface Validator {
 
    staticfinalintINVALID_ACCOUNT_USERNAME = 1000; //"Invalid username: must 6-12 characters.";
    staticfinalintINVALID_ACCOUNT_PASSWORD = 1001;
 
    staticfinalintAPPLICATION_ERROR = 909091;
 
    void validate() throws ValidateException;
 
}
publicclass Account implements Validator {
 
    publicstaticfinalintSTATE_ACTIVE = 0;
    publicstaticfinalintSTATE_INACTIVE = 1;
    publicstaticfinalintSTATE_LOCKED = 2;
 
    // blogger login info:
    privateintaccountId;
    private String username;
    private String password;
    private String email = "";
 
    private Date createdDate;
 
    // personal setting:
 。。。。。。。。。。。。。。
 
    publicvoid validate() throws ValidateException {
         if(username==null || username.equals(""))
            thrownew ValidateException("用户名不能为空");
//        if(!username.matches("[a-z0-9]{6,20}"))
//            throw new ValidateException(INVALID_ACCOUNT_USERNAME);
        if(password==null || password.equals(""))
            thrownew ValidateException(“密码不能为空”);
     
    }
}
3.sturts中公共的处理类:
 
publicclass AppExceptionHandler extends ExceptionHandler {
    /**
     *Loggerforthisclass
     */
    privatestaticfinal Log logger = LogFactory
           .getLog(AppExceptionHandler.class);
 
    public ActionForward execute(Exception ex,
            ExceptionConfig ae,
            ActionMapping mapping,
            ActionForm formInstance,
            HttpServletRequest request,
            HttpServletResponse response) throws ServletException {
       ActionForward forward = null;
       if (ae.getPath() != null) {
            forward = new ActionForward(ae.getPath());
        } else {
            forward = mapping.getInputForward();
        }
        String url = request.getRequestURL().toString();
            String param = request.getQueryString();
            if(param!=null && !param.equals(""))
                url = url + "?" + param;
        String message =ex.getMessage();
 
            request.setAttribute("url", url);
            request.setAttribute("message", message);
   
       return forward;
    }
 
}
4Struts-config.xml中的配置:
 <global-exceptions>
        <exception
            key="error.sysError"
            type="java.lang.Exception"
            handler="com.yourcompany.struts.action.AppExceptionHandler"
            path="/error.jsp"/>      
</global-exceptions>
 
5Error.jsp页面内容:
<%@ page contentType="text/html;charset=GBK" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ERROR</title>
<link href="normal.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
 <tr>
    <td align="center"><table width="570" border="01" cellpadding="03" cellspacing="0" bordercolor="#A5B6D1">
      <tr>
        <td bgcolor="#A5B6D1">Error</td>
      </tr>
      <tr>
        <td bordercolor="#FFFFFF">
          错误信息:</td>
      </tr>
      <tr>
        <td bordercolor="#FFFFFF">
          <b>请求URL</b>: <c:out value="${url}"/><br/>
          <b>错误原因</b>: <c:out value="${message}"/>
        </td>
      </tr>
      <tr>
        <td bordercolor="#FFFFFF">
          Please go back to the previous page and check your input.
          Also you can contact the <a href="mailto:asklxf@163.com">author</a> if you are sure this is a bug of the system. Thank you!
        </td>
      </tr>
    </table></td>
 </tr>
</table>
</body>
</html>
  
注意事项:<%@ page errorPage="/common/errorpage.jsp" %>
表示如果是页面有错误就会转到errorpage.jsp中去。
同时errorpage.jsp中要声明<%@ page isErrorPage="true" %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值