我想通过jsp向浏览器显示一个特定的错误

时间:2013-03-22 04:00:37

标签: jsp

我正在创建一个javabean类和error.jsp。问题是我想在error.jsp中显示特定的Exception。所以我做了错误变量,我希望错误来自错误变量,所以我可以向用户显示....

package mypack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; 
import java.sql.ResultSet;

public class StudentBean {
    int roll, marks;
    String name, grade;
    String error;
    public String getError() {
        return error;
    }
    public void setError(String error) {
        this.error = error;
    }
    public int getMarks() {
        return marks;
    }
    public String getName() {
        return name;
    }
    public String getGrade() {
        return grade;
    }
    public int getRoll() {
        return roll;
    }
    public void setRoll(int roll) {
        this.roll = roll;
    }
    public boolean lookup() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
            PreparedStatement pst = con
                    .prepareStatement("select * from student where roll=?");
            pst.setInt(1, roll);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                name = rs.getString(1);
                roll = rs.getInt(2);
                marks = rs.getInt(3);
                grade = rs.getString(4);
            }
        } catch (Exception e) {
            System.out.println("Generated Exception " + e);
            error = e.getMessage();
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试处理所有可能出现在代码中的异常,例如

 catch (NullPointerException e) {
            System.out.println("Generated Exception is NUllpointer" + e);
            error = e.getMessage();
}
 catch (OtherException e) {
            System.out.println("OtherException " + e);
            error = e.getMessage();
}