This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Forwarding from servlet to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Forwarding from servlet to JSP" Watch "Forwarding from servlet to JSP" New topic
Author

Forwarding from servlet to JSP

Raycho Belchilov
Greenhorn

Joined: Mar 24, 2012
Posts: 7
Hi, my question is about forwarding from a servlet to a JSP. Here is my servlet:

So if the session has expired the servlet redirects to login.jsp. The problem is that codeA is also executed. I want if the session has expired, the servlet to redirect to the JSP and codeA to be ignored. CodeA must be executed only when the session is valid. I know I can do it this way:

However putting my long code in an else clause makes it more difficult to read. That's why I am asking if there is another way - a command that forwards to a JSP and discards the remaining code (like <jsp:forward page="/..."/> in JSPs).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

This is not a question about servlets, and it's not a question about forwarding to a JSP. The code in a servlet is Java code and it follows all of the rules of Java code. Nothing special happens in your Java code because it is in a servlet.

In other words you already know the answer to this. An alternative to

is


Or you could put your "long code" into another method and call it from this one... there's lots of Java options you could use. Just don't get distracted by the fact that your Java code happens to be in a servlet.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
You should probably use Filter to check if User is logged out or not. Otherwise you would have to duplicate the check in every servlet in your application.
Raycho Belchilov
Greenhorn

Joined: Mar 24, 2012
Posts: 7
Thanks for the answers! Using a filter is really the best solution in this case.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forwarding from servlet to JSP
 
Similar Threads
GET method working but POST method not working
Forwarding A Servlet Response in the URL
regarding session transfer from servlet to jsp
How to know login & logout status of user
Solution for repeating function in all servlets.