0

When I run the script, the browser proceeds to perpetually open new tabs until I end the process in the task manager. Shouldn't this loop only open the browser once? Even if you ignore the while loop and only focus on the browser request I get the same behavior. Does this have to do with the try/catch? What's going wrong here?

Desktop d = Desktop.getDesktop();       

Boolean doOnce = false;

while (doOnce == false) {
    try {
        d.browse(new URI("http://localhost"));
        doOnce = true;
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
6
  • 1
    why you want to execute a loop once? why loop then? Commented May 18, 2016 at 9:31
  • Because I thought my server was doing something funny, and by providing a flow-control structure I could stop whatever was going down. Commented May 18, 2016 at 9:35
  • You can try do-while loop if you have to execute the code atleast once. Commented May 18, 2016 at 9:37
  • Just a wild guess.... but the URI you call in d.browse doesn't refer to the servlet page itself, does it? Because that would kind of explain the behaviour as it would create some kind of infinite recursion. Commented May 18, 2016 at 9:44
  • I don't understand your construct. You create a new browser tab on your server machine if someone opens an URL on your server. Why do you want to do that? Commented May 18, 2016 at 9:45

3 Answers 3

2

If you want to execute the try/catch block only once in while you should put the doOnce in your finally block :

try 
{
    d.browse(...);
} catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}finally{
doOnce = true;
}

but if you want to exit from while when an exception occurred you should put it in catch block :

try 
{
    d.browse(...);
} catch (URISyntaxException e) {
     doOnce = true;
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Would the fact that it's in my doGet(HttpServletRequest... method have anything to do with multiple executions?
I think the second one is suitable for your case
Okay I actually get what's going on now. Every time the new browser is opened it executes the command again.
2

In case d.browse(....) throws an exception and doOnce=true will never executed.

Move doOnce=true into a finally block and it should work.

1 Comment

what would be the point of a "loop" that only ever gets executed once?
0
try 
{
    d.browse(...);
    doOnce = true;
} catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

If your call to browse above raises an exception, you will never set the exit flag.

Why are you doing this in a loop? If your intent is to only do this step once, do it once as a statement rather than using a flow-control structure.

1 Comment

d.browse("... alone still opens the browser a million times. Could this have to do with Tomcat?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.