aspose file tools*
The moose likes JSP and the fly likes Page is still executed even after response.sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Page is still executed even after response.sendRedirect()" Watch "Page is still executed even after response.sendRedirect()" New topic
Author

Page is still executed even after response.sendRedirect()

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
In the beginning of a JSP page, I checked for a variable called "sessionLogin" everytime a user click a submit button in a form. And if this variable is empty or NULL then I redirect to another page called "invalidLogin.jsp".

I have checked and the redirection is working ok. But the second if statement below keeps getting executed even though the page is redirected.
I don't know why. Could someone please advise? I'm probably missing something trivial here.

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The sendRedirect method does not halt the execution of your method.
You should either branch your code in such a way that the call to sendRedirect is the last statement in your method or explicitly call return; after calling sendRedirect.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The same applies to RequestDispatcher.forward.
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
But I've done a test below and the page is redirected to test2.jsp immediately and the alert statement in test1.jsp is not being executed. Why is that?

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Because the sendRedirect method was called.

Remember, these are just Java methods calling one another.
The rules don't change just because they're servlets (or servlets generated by JSPs).

sendRedirect gets called, as soon as it's done, the code under is executed.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

The alert() is JavaScript and has nothing to do with your JSP. It is not executed at the same time as the JSP which executes on the server.

Perhaps this article would be helpful.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
Thanks for the help.

Let me read the article.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Page is still executed even after response.sendRedirect()