wood burning stoves 2.0*
The moose likes Servlets and the fly likes sendRedirect (), forword () Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect (), forword ()" Watch "sendRedirect (), forword ()" New topic
Author

sendRedirect (), forword ()

deepak bansal
Ranch Hand

Joined: Feb 25, 2001
Posts: 33
Hi All,
I am facing a problem regarding the sendRedirect() and forword() .
I want to use both of them in my servlet method in the doPost() method . I am doing following two things in the doPost method of servlet in this order---->
1)First i am using the senRedirect() Method to send the request to a servlet to Invalidate the Session.
2)I am displaying a jsp page to the browser using the forword method.
The problem is that session is getting invalidated properly but its giving the IllegalStateException when it comes to the forward method.
Thanks
Deepak
------------------
Be Truthful to yourself!


Be Truthful to yourself!
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Why can't you invalidate the Session on the fly?
I don't think sendRedirect() forwards anything to the destination URL. Sure you could use GET parameters when building the URL but, the request will not be forwarded as in forward().
Probably the Request is terminated when you execute sendRedirect().
The API says "After
using this method, the response should be considered to be committed and should not be written
to."


I'd like to see what other have to say...


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
You're right, Tony. In a single doPost() method it's "either/or" not "both and".
Kyel
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
deepak bansal
Ranch Hand

Joined: Feb 25, 2001
Posts: 33
Hi Both,
My problem is that i am working in the clustered environment and if the same person logins from the different machine i have to kill the previous session .I am stroring the information into the database as user logins first time and using this if he logins agains. In the single login method i am using the sentRedirect method to send the request to a InvalidateServlet which kills the session (Even the previous session is in the different machine in the cluster environment).I want the Control back to my login function to show the user desitrable page(Using the forward method).
Thanks & Regads
Deepak
------------------
Be Truthful to yourself!
James Hobson
Ranch Hand

Joined: Aug 28, 2001
Posts: 140
That isnt going to work - sendRedirect sends a response to the Client (the actual browser) asking them to send a new request to the InvalidateServlet. There is no way that you will be able to sensibly re-use the initial request by further processing. If you do manage to get this to work, you will have all kinds of thread safety problems.
You should probably do this in your business logic layer, not servlet for this functionality.
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Deepak - I think I have a solution for you. Why not do two forwards, instead of doing a redirect? All you have to do is pack your final destination page/servlet name using req.setAttribute( "finalJSP", "myJSP.jsp" ), forward to InvalidateServlet, invalidate the session, then have InvalidateServlet forward to req.getAttribute( "finalJSP" ). How are you detecting that a user has a session on a different machine? Or are you just attempting to kill any that exist for that user when a new session is created? Good luck!

CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
deepak bansal
Ranch Hand

Joined: Feb 25, 2001
Posts: 33
Hi Gerry/James,
I cannot use the forward twice because i am sending the request to different machine in the cluster .forward method can be used only within the context of the servlet .So i have to use the sendRedirect(). As faor as the session infromation is concern i am keeping it into the database.
Thanks & regadrs
Deepak
 
Don't get me started about those stupid light bulbs.
 
subject: sendRedirect (), forword ()
 
Similar Threads
establishing a persistent cart
what is differance between the sendredirect and forword method?
to redirect.
Can sendRedirect method hit a different server instance?
Servlet to servlet