File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Despite if condition being true, both if and else statements execute in jsp code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Despite if condition being true, both if and else statements execute in jsp code" Watch "Despite if condition being true, both if and else statements execute in jsp code" New topic
Author

Despite if condition being true, both if and else statements execute in jsp code

rohan chakravarthy
Greenhorn

Joined: Apr 13, 2012
Posts: 7


Thats the javascript code in which I'm using jsp to access the database and verify the username and password. On entering a correct username and password, the page does get redirected. However, the alert "Invalid Credentials" also shows. How is this possible? That would mean both the if and else statements are getting executed :confused:
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

This is why Java inside JSP is a very very bad idea!

Take a look at the generated HTML post-JSP processing.

WP

rohan chakravarthy
Greenhorn

Joined: Apr 13, 2012
Posts: 7
I'm sorry I'm very new to this. How do I check that?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

Right-click on what you see in the browser (the output of that code) and choose the option which is something like "View Page Source".
rohan chakravarthy
Greenhorn

Joined: Apr 13, 2012
Posts: 7
Oh ok. Got it. It only shows the alert. Why does this happen? Is there a solution for it?

I'd really appreciate some advice on how to go about implementing this correctly :)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Step 1: No Java code in a JSP. None., Zero. Nada. Ever. This isn't 2001 anymore.

Replace any Java code in the JSP by refactoring it into a controller and with the JSTL and EL.

Then we can continue.



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

The basic idea of the "controller" is that you should do all of the decision-making code before you start producing any HTML (or Javascript) output. In your code, for example, you write out part of a Javascript function and then (if you accept the authentication) you redirect to a different URL. You got away with it because you didn't write out much data, but if you had written more, enough to fill the output buffer, then redirecting would have caused an exception.

Here's what you should do: First do all of your database processing, including even closing your resources. Use variables which keep track of what to do next, if you need them. (This is the "controller" part and as Bear says, it should go in a servlet.)

Then redirect (or maybe forward) to the next page. Here you might make a decision: if the authentication was okay then redirect to "userpage", otherwise redirect (or forward) to "errorpage". The "errorpage" JSP would generate the Javascript function which includes the appropriate alert.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

This article explains it all.
rohan chakravarthy
Greenhorn

Joined: Apr 13, 2012
Posts: 7
Oh I finally got it! Paul, your explanation was great! Thanks a lot! :)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

Glad to hear it! (Because I had no idea why your original code was behaving the way it was.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Despite if condition being true, both if and else statements execute in jsp code
 
Similar Threads
Internal Servlet Error:
Login Authentication & sendRedirect
Jsp code doubt
Invalid user error message
creating website basic question