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

标签 java jsp servlets dao

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

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

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

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

供引用: 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 上下文监听器来了=>

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");
}

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

最佳答案

您可以抛出异常并在 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>

关于java - 数据库连接失败时如何重定向到错误页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21424931/

相关文章:

java - 使用servlet提交时数据库中没有数据

java.io.IOException : failed to decrypt safe contents entry: javax. crypto.BadPaddingException:给定的最终 block 未正确填充

java - 无法从数据库获取数据

java - 如何从MySql表列中检索特定字段的总和到变量中

java - Dispatcher.forward 导致无限循环

servlets - JBoss 5.1.0 GA servlet API 版本

java - 如何将 2 个不同 JTextField 中的 2 个元素添加到一个 JList

java - 将请求解释为 JSP 而不是通过 servlet

java - 将java代码注入(inject)<aui :select> control (with <% %> or <%= %> )的问题

java - 谷歌应用引擎错误 : Fetch in a thread that is neither the original request thread nor a thread created by ThreadManager