How to stop JSP Execution if class constructor failed
OK, I call the database constructor which will try connecting to the database on the class' constructor:
JSP page:
Code:
Database db = new Database();
System.out.print("Can't print this!");
Database class:
Code:
public Database(){
try{
Class.forName(driver);
Database.conn = DriverManager.getConnection(URL, user, password);
}catch(Exception e){
System.out.print("Couldn't connect to the database!");
}
}
OK, what i want to do now, is to halt the execution on the page if the connection fails and there's an Exception and only prints the custom message from the Catch.
I have done some research since yesterday but i might be failing on using the right keywords, :(...
How would i proceed into doing this?
Thanks in advance!
Re: How to stop JSP Execution if class constructor failed
Would return do what you want?
Re: How to stop JSP Execution if class constructor failed
You mean like this?
Code:
public Database(){
try{
Class.forName(driver);
Database.conn = DriverManager.getConnection(URL, user, password);
}catch(Exception e){
return;
}
}
Nope, it still continues the code, printing "Can't print this!".
Re: How to stop JSP Execution if class constructor failed
Set a flag in the class and test it after the constructor exits.
Have the constructor throw an exception that the caller can catch.
Re: How to stop JSP Execution if class constructor failed
Quote:
Originally Posted by
Norm
Set a flag in the class and test it after the constructor exits.
Have the constructor throw an exception that the caller can catch.
Errrr, what is a flag on the class? Something like "Set the Flag in Java - Stack Overflow"?
Re: How to stop JSP Execution if class constructor failed
What I call a flag is a variable that has values to show if an event has happened or not. Usually its a boolean that is false before the event and set to true when the event happens.
Re: How to stop JSP Execution if class constructor failed
Oh yes, i'm glad you weren't referring to the link i mentioned! :D
Anyways... here's what i did for debugging... is this what you mean?
Database class:
Code:
public Database(){
try{
Class.forName(driverLocalhost);
Database.conn = DriverManager.getConnection(urlLocalhost, userLocalhost, passwordLocalhost);
Main.out.print("FFS!<br />");
}catch(Exception e){
e.printStackTrace();
Main.out.print("YEY!<br />");
return;
}
}
And here's the output:
Code:
YEY!
Can't print this!
The phrase "Can't print this!" is still appearing even though the return is being executed...
Anyways, i'll explain what i'm trying to achieve so that, perhaps this way, you can suggest some other approach.
What i'm doing is this:
1 - JSP file loads the Database class
Code:
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="Config.Main"%>
<%@ page import="Database.Database"%>
<%
Main cfg = new Main(request, response);
Database db = new Database();
Main.out.print("Can't print this!");
%>
On the "Can't print this!", goes the HTML code for the page the user is on.
The database class tries to connect to the database specified on URL. If it fails, it must NOT display the page and show instead, "CAN'T CONNECT TO THE DATABASE", or something along those lines. If the connection succeeds, the jsp will execute the HTML code normally and display the page to the user.
I could do this by creating another function on the Database class and make that connect on the database and return a boolean value. Then, with this boolean i'd make an if statement putting all the page code inside of it.
But I would like this approach better, like a php die():
Database class
Code:
function __construct(){
if ( !$this->connect($this->host, $this->user, $this->pass, $this->base) ){
die();
}
}
However, i'm also not sure if that would work on PHP...
Any insights?