数据库连接失败时如何重定向到错误页面?

时间:2014-01-29 07:41:36

标签: java jsp servlets dao

我正在尝试在我的应用程序中实现一项功能,例如,在第一次请求容器时,如果数据库无法连接,它应该重定向到错误页面而不是显示index.jsp

  1. 我有一个单例DBConnectionManager类,它在构造函数中处理数据库连接,也是一个返回连接对象的方法。

  2. 我有一个servlet Context Listener。因此,当初始化上下文时,它将调用DBConnectionManager构造函数并初始化数据库连接。我将在我的所有模型DAO中检索并使用相同的连接对象。

  3. 现在我的问题是如何处理连接错误并将其从error.jsp或servlet上下文侦听器重定向到DBConnectionManager页面。我应该在哪里放置重定向条件。

    供参考: DBConnectionManager.java =>

    public DBConnectionManager(String url,String user,String pass)
    {
    //constructor called by servlet context listener.
    this.DBURL=url;
    this.user=user;
    this.pass=pass;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e) {
    log.info("Class Not Found :"+e);
    return;
    }
    try {
    log.info("-------url recieved :"+url);
    log.info("-------username received :"+user);
    log.info("--------password received :"+pass);
    con = DriverManager.getConnection(url,user,pass);
    log.info("con initialised..");
    } 
    catch (SQLException e) {
    //Exception caught here successfully.
    log.info("Problem in connecting :"+e);
    return;
    }
    
    if (con != null) {
    log.info("Connected Successfully to database");
    } else {
    log.info("Mysql not connected.:<");
    }
    
    public Connection getConnection()
    {
     //Method called in every DAOmodel to return the instance of the connection object.
     //Where DAO uses this connection object to query database.
    return this.con;   
    }
    

    这是我的Servlet Context Listener =&gt;

    public void contextInitialized(ServletContextEvent servletContextEvent) {
            ServletContext ctx=servletContextEvent.getServletContext();
            String url=ctx.getInitParameter("DBURL");
            String user=ctx.getInitParameter("DBUSER");
            String pass=ctx.getInitParameter("DBPWD");
    
            //Constructor called here
            DBConnectionManager dbManager= new DBConnectionManager(url,user,pass);
            log.info("printing db manager::"+dbManager);
            ctx.setAttribute("DBManager", dbManager);
            log.info("dbmanager Initialized");
    
            }
    
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
            ServletContext ctx=servletContextEvent.getServletContext();
            DBConnectionManager dbManager=      (DBConnectionManager)ctx.getAttribute("DBManager");
            dbManager.closeConnection();
            log.info("dbmanager destroyed and database connection closed");
    }
    

    如果您对我的问题有任何疑问,希望这可以解决。

4 个答案:

答案 0 :(得分:1)

您可以抛出异常并为web.xml之类的错误定义500页;

<error-page>
    <error-code>500</error-code>
    <location>/error.html</location>
</error-page>

当您的系统出错时,将呈现error.html。对于其他类型错误,您可以定义;

<error-page>
    <!-- Missing login -->
    <error-code>401</error-code>
    <location>/error-401.html</location>
</error-page>
<error-page>
    <!-- Forbidden directory listing -->
    <error-code>403</error-code>
    <location>/error-403.html</location>
</error-page>
<error-page>
    <!-- Uncaught exception -->
    <error-code>500</error-code>
    <location>/error-500.html</location>
</error-page>
<error-page>
    <!-- Unsupported servlet method -->
    <error-code>503</error-code>
    <location>/error-503.html</location>
</error-page>

答案 1 :(得分:1)

如果要处理所有数据库异常,则需要将其作为一些 SomeDbException 抛出并将其添加到web.xml。另请查看以下doc

<error-page>
    <exception-type>SomeDbException</exception-type>
    <location>/error.html</location>
</error-page>

答案 2 :(得分:0)

无论哪种错误,您都可以随时重定向到任何其他页面:

<%
try {
    // do whatever you want (Connect to database ...)
} catch(Exception ex) {
    // logging ...

    response.sendRedirect(..);
    return;
}
%>

<强>更新

抱歉,但我对你的问题有点困惑。但是:

  • 如果在servlet init上发生了错误,你的单音类就在其中,只要留下这个错误,因为应用程序无法启动,用户应该修复它,你可以生成任何帮助用户的日志来弄清楚是什么是问题!
  • 对于实用程序和一些特殊情况,Singleton并不错,但对于Web应用程序,您应该尽可能地避免它。您可以创建一个实例并将其放在会话上,如果它对每个用户都是唯一的,并且如果它在整个系统中是唯一的,则可以将它放在servlet Context上。 getServletContext()。setAttribute(“my”,sample); 希望它有所帮助

我希望这可以帮到你

答案 3 :(得分:0)

@Prithviraj:答案如下:假设您有Index.jsp页面,其中连接失败并且您得到一些异常。 因此,在该页面(Index.jsp)中,您需要使用errorPage指令的<%@ page %>属性。像这样:<%@ page errorPage="Error.jsp %>Error.jsp页面中,您需要使用isErrorPage指令的<%@ page %>属性。 像这样:<%@ page isErrorPage="true" %>。 现在当连接失败并发生异常时会发生什么,然后自动控制将被重定向到Error.jsp页面,您不必编写重定向逻辑。

我相信它会对你有所帮助。