【问题标题】:How to redirec to to HTML if exception occured如果发生异常,如何重定向到 HTML
【发布时间】:2012-05-01 03:30:43
【问题描述】:

我一直在尝试寻找一种在发生 SQLException 时重定向到 HTML 文件的方法。我给你举个例子。

我的连接类是这样的:

public class DBConection{
    Connection con = null;
    public DBConnection() throws RuntimeException{      
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            String user = "root";
            String pass = "12345";
            String db = "java";
            con = DriverManager.getConnection("jdbc:mysql://localhost/" + db, user, pass);
        }catch (ClassNotFoundException ex) {
            throw new RuntimeException();
        }catch (SQLException ex) {
            throw new RuntimeException();
        }
    }
}

我从 Servlet 类中调用它,

public class ElectionsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String RETURN_PAGE = "index.jsp";

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        DBConnection con = new DBConnection();
        response.sendRedirect(RETURN_PAGE);
    }
}   

如果在 DBConnection 期间发生错误,我想将其重定向到我的 RETURN_PAGE。有人可以帮帮我吗?

【问题讨论】:

    标签: html database exception servlets


    【解决方案1】:

    只需使用内置的容器管理的异常处理工具。您需要将捕获的异常重新抛出为ServletException,并在web.xml 中将错误页面定义为<error-page>

    您只需要相应地更改您的数据库访问逻辑。它远非理智、安全和高效。异常处理也很奇怪。鉴于您使用的是已弃用的 MySQL JDBC 驱动程序类名称,您似乎已经从完全过时的资源中删除了它。

    这是一个基本的启动示例:

    public final class Database {
        
        private static final String DRIVER = "com.mysql.jdbc.Driver";
        private static final String URL = "jdbc:mysql://localhost/java";
        private static final String USER = "root";
        private static final String PASS = "12345";
    
        static {      
            try {
                Class.forName(DRIVER);
            }
            catch (ClassNotFoundException e) {
                throw new ExceptionInInitializerError("The JDBC driver is missing in classpath!", e);
            }
        }
    
        private Database() {
            // Don't allow construction.
        }
    
        public static Connection getConnection() throws SQLException {
            return DriverManager.getConnection(URL, USER, PASS);
        }
    
    }
    

    最后在servlet中使用和处理如下:

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Connection connection = null;
        // ...
    
        try{
            connection = Database.getConnection();
            // ...
        } 
        catch (SQLException e) {
            throw new ServletException("DB fail!", e);
        }
        finally {
            // ...
            if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
        }
    }
    

    (最好将 DB 作业包装在 DAO 类中,该类又会抛出 SQLException,但这是另一个问题)

    并在web.xml中声明错误页面如下:

    <error-page>
        <exception-type>java.sql.SQLException</exception-type>
        <location>/WEB-INF/errorpages/database.jsp</location>
    </error-page>
    

    您当然也可以使用&lt;location&gt;/index.jsp&lt;/location&gt;,但这对最终用户来说不是很友好,因为他返回那里的原因完全令人困惑。而是将其作为链接放在database.jsp 错误页面中,以及一些用户友好的内容,例如

    抱歉,与 DB 通信时出现不可恢复的问题。请点击以下链接返回索引页面。

    另见:

    【讨论】:

      【解决方案2】:

      问题已经回答了,但我会添加一些cmets:

      • 我认为不需要捕获 SQLExceptionClassNotFoundException 只是为了重新处理 RuntimeException 实例。如果您打算在 DBConnection 类中引发此异常时不执行任何操作,只需将 throws 添加到构造函数签名中即可。这样,编译器将在使用构造函数时检查是否添加了 try-catch 块。
      • 无需将throws RuntimeException 添加到您的构造函数签名中。 RuntimeException 是一个未经检查的异常。

      【讨论】:

        【解决方案3】:

        将 try-catch 块添加到您的 doPost 方法中,如果 try 块中发生任何异常,则捕获该异常并重定向到 RETURN_PAGE

        public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                try{
                DBConnection con = new DBConnection();
                }catch(Exception e){
                response.sendRedirect(RETURN_PAGE);
               }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-13
          • 2015-01-04
          • 2016-03-23
          • 2012-10-19
          • 2023-03-24
          • 2011-08-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多