我正在尝试在我的应用程序中实现一项功能,例如,在第一次请求容器时,如果数据库无法连接,它应该重定向到错误页面而不是显示index.jsp
。
我有一个单例DBConnectionManager
类,它在构造函数中处理数据库连接,也是一个返回连接对象的方法。
我有一个servlet Context Listener。因此,当初始化上下文时,它将调用DBConnectionManager
构造函数并初始化数据库连接。我将在我的所有模型DAO中检索并使用相同的连接对象。
现在我的问题是如何处理连接错误并将其从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");
}
如果您对我的问题有任何疑问,希望这可以解决。
答案 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;
}
%>
<强>更新强>
抱歉,但我对你的问题有点困惑。但是:
我希望这可以帮到你
答案 3 :(得分:0)
@Prithviraj:答案如下:假设您有Index.jsp
页面,其中连接失败并且您得到一些异常。
因此,在该页面(Index.jsp
)中,您需要使用errorPage
指令的<%@ page %>
属性。像这样:<%@ page errorPage="Error.jsp %>
在Error.jsp
页面中,您需要使用isErrorPage
指令的<%@ page %>
属性。
像这样:<%@ page isErrorPage="true" %>
。
现在当连接失败并发生异常时会发生什么,然后自动控制将被重定向到Error.jsp
页面,您不必编写重定向逻辑。
我相信它会对你有所帮助。