全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1974
推到 Plurk!
推到 Facebook!

如何在JSP中使用THROWS

尚未結案
fangwy
中階會員


發表:99
回覆:139
積分:62
註冊:2003-09-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-02-04 10:21:58 IP:218.5.xxx.xxx 未訂閱
我的JSP如下
 
<%@page language="java" contentType="text/html;charset=GB2312"%>
<HTML><HEAD><TITLE>自定义异常类的测试 </TITLE></HEAD><BODY>
<%@ page import="com.macmillan.jspln24.hour14.*"%>

<%String username = request.getParameter("username");
  String password = request.getParameter("password");
  try{
      if(username!=null) vaildate.checkUsername(username);
  }catch(InvaildUserNameException e){%>
      对不起,您输入的用户名<%=username%>太短了
<%}
  try{
      if(password != null) vaildate.checkPassword(password);
  }catch(InvaildPasswordException e){%>
      对不起,您输入的密码<%=password%>太短了
<%}%>
请登录:
<FORM ACTION = RaisingExceptionInClass.jsp METOD = POST>
Username: <INPUT TYPE = TEXT NAME = username> 
Password: <INPUT TYPE = TEXT NAME = password> 
<INPUT TYPE = SUBMIT VALUE = Login>
</FORM>
</BODY></HTML>
可是在IE中打開時提示錯誤: An error occurred at line: 5 in the jsp file: /RaisingExceptionInClass.jsp Generated servlet error: D:\java\Tomcat 5.0\work\Catalina\localhost\jspscjc\org\apache\jsp\RaisingExceptionInClass_jsp.java:60: exception com.macmillan.jspln24.hour14.InvaildUserNameException is never thrown in body of corresponding try statement }catch(InvaildUserNameException e){ ^ An error occurred at line: 11 in the jsp file: /RaisingExceptionInClass.jsp Generated servlet error: D:\java\Tomcat 5.0\work\Catalina\localhost\jspscjc\org\apache\jsp\RaisingExceptionInClass_jsp.java:69: exception com.macmillan.jspln24.hour14.InvaildPasswordException is never thrown in body of corresponding try statement }catch(InvaildPasswordException e){ ^ 2 errors 不知在JSP中怎麽加入THROWS
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-02-04 14:27:20 IP:61.64.xxx.xxx 未訂閱
jsp我是已經四五年沒在寫了.不過一般的java例外的投擲方式不外如下 1.定義自訂例外類別:
class WrongPassword extends Exception {
   ....
}
class NoAddress extends Exception{
  ...
}
2.在處理商業邏輯中,視必要,丟出例外,以供caller決定如何反映
==========this is the callee==================================
class MemberHandler{
    ...
    ...
   public void doRegMember(String password,String address)throws
     WrongPassword,NoAddress,Exception{
       ...
        ...
      if(password格式有誤)throw new WrongPassword();
      if(address==null || address格式有誤)throw new NoAddress();
      //開始進行資料庫作業,可能會丟出其他例外,如java.sql.SQLException...
      ...
      ...
   }
}
====================this is caller,can be jsp or swing or even servlet/ejb======
  class GUI {
    public void getMemberData(){
     ...
      ....
     MemberHandler handler=new MemberHandler();
     try{
           String pwd=textPwd.getText();// or get from request.getParameter("passwod");
           String add=textAdd.getText();
           handler.doRegMember(pwd,add);
           //process ok,
      }catch(WrongPassword wpe){
           //re-dirct to the password wrong exception message to notify user
           //or use JDialogBox to notify user password is not correct
             return;
      }catch(NoAddress nae){
           //re-dirct to the address exception message to notify user
           //or use JDialogBox to notify user address is not correct
           return;
      }catch(Exception exp){
         //系統錯誤,請洽系統程式設計師
     }
    }
  }
fangwy
中階會員


發表:99
回覆:139
積分:62
註冊:2003-09-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-02-21 09:32:10 IP:218.5.xxx.xxx 未訂閱
谢谢neoart兄,在JAVA类中处理异常我明白,我的意思是在JSP中有没有什么方法方便的处理异常,就如引入一个错误页一样,只需一条语句说明就可以,而不要在每个出错处都去打开错误页.如果没有哪我只能将try..catch写入JSP代码中.
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-02-21 09:52:14 IP:61.64.xxx.xxx 未訂閱
引言: 谢谢neoart兄,在JAVA类中处理异常我明白,我的意思是在JSP中有没有什么方法方便的处理异常,就如引入一个错误页一样,只需一条语句说明就可以,而不要在每个出错处都去打开错误页.如果没有哪我只能将try..catch写入JSP代码中.
<%@ page errorPage="errorpge.jsp" %>
參考: http://www.chinabs.net/jsp/default.asp?infoid=236 http://www.javaworld.com.tw/jute/post/view?bid=6&id=48369&sty=1&tpg=1&age=0 http://www.caterpillar.onlyfun.net/PmWiki/pmwiki.php/JSPServlet/ExceptionImplicitObject
系統時間:2024-05-09 14:11:05
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!