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

 
在服务器端进行异常处理:
1. 各种类型的异常类:
public class ValidateException extends RuntimeException {
 
    public ValidateException() {}
 
    public ValidateException(String msg) {
        super (msg);
    }
 
    public ValidateException( int msgId) {
        super ();
 
    }
 
 
}
public class CreateException extends RuntimeException {
 
    public CreateException() {}
 
    public CreateException(String msg) {
        super (msg);
    }
 
    public CreateException(Throwable throwable) {
        super (throwable);
    }
 
}
 
2.定义VO中的验证接口及方法:
public interface Validator {
 
    static final int INVALID_ACCOUNT_USERNAME  = 1000; //"Invalid username: must 6-12 characters.";
    static final int INVALID_ACCOUNT_PASSWORD  = 1001;
 
    static final int APPLICATION_ERROR = 909091;
 
    void validate() throws ValidateException;
 
}
public class Account implements Validator {
 
    public static final int STATE_ACTIVE = 0;
    public static final int STATE_INACTIVE = 1;
    public static final int STATE_LOCKED = 2;
 
    // blogger login info:
    private int accountId ;
    private String username ;
    private String password ;
    private String email = "" ;
 
    private Date createdDate ;
 
    // personal setting:
  。。。。。。。。。。。。。。
 
    public void validate() throws ValidateException {
         if ( username == null || username .equals( "" ))
            throw new ValidateException( " 用户名不能为空 " );
//        if(!username.matches("[a-z0-9]{6,20}"))
//            throw new ValidateException(INVALID_ACCOUNT_USERNAME);
        if ( password == null || password .equals( "" ))
            throw new ValidateException( “密码不能为空” );
     
    }
}
3.sturts 中公共的处理类:
 
public class AppExceptionHandler extends ExceptionHandler {
    /**
      * Logger for this class
      */
    private static final 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;
    }
 
}
4 Struts-config.xml 中的配置:
  < global-exceptions >
        < exception
            key = "error.sysError"
            type = "java.lang.Exception"
            handler = "com.yourcompany.struts.action.AppExceptionHandler"
            path = "/error.jsp" />       
</ global-exceptions >
 
5 Error.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" %>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值