Results 1 to 7 of 7
  1. #1
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Question 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:
    Java Code:
    Database db = new Database();
    
    System.out.print("Can't print this!");
    Database class:
    Java 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!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: How to stop JSP Execution if class constructor failed

    Would return do what you want?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: How to stop JSP Execution if class constructor failed

    You mean like this?

    Java 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!".
    Last edited by KingdomX; 08-19-2012 at 10:40 PM. Reason: ponctuation

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: How to stop JSP Execution if class constructor failed

    Quote Originally Posted by Norm View Post
    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"?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default 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:
    Java 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:
    XML 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
    Java 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
    PHP 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?
    Last edited by KingdomX; 08-21-2012 at 04:15 AM.

Similar Threads

  1. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  2. Threads execution stop
    By jyotigupta6 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-31-2008, 01:41 PM
  3. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •