• Post Reply Bookmark Topic Watch Topic
  • New Topic

Session and RequestDispatcher question

 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to centralize the creation of sessions in the application. Look at this piece of code:

session = request.getSession(false);
if (session == null) {
System.out.println("Session is null, send to login page");
RequestDispatcher rd = request.getRequestDispatcher("/jsp/Login.jsp");
rd.forward(request,response);
System.out.println("service(): after forwarding to JSP page");
return; // Why do I need to do this ? the forward should not get the control back here
}
else {
System.out.println("service(): Session is available is not null.");
}

1. When the request comes for the first time, Lgoin.jsp is displayed. But, when a refresh is hit on the browser, a session is available and is NOT null.Confusing. How is that ?

2. Am assuming the forward method of requestDispatcher gives away the control of the servlet ...but what I observed is that the control stays with the servlet. I see the statements printed..and so I had to use return..wierd.Am I doing anyting wrong here ?
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. When the request comes for the first time, Lgoin.jsp is displayed. But, when a refresh is hit on the browser, a session is available and is NOT null.Confusing. How is that ?

check out following steps
1) Browser makes a request to server
2) Server retrives session id from request ( if any)
3) if no session was alive for this client then getSession(false)will
return null but before sending response back to client a session
will be created for client and session id will be sent back with response.
hence when you make first request there is no session so you are re-
directed to login page along with session id,and on hiting refresh button
this session id will be sent to server so getsession(false) will return session associated with this session id.

2. Am assuming the forward method of requestDispatcher gives away the control of the servlet ...but what I observed is that the control stays with the servlet. I see the statements printed..and so I had to use return..wierd.Am I doing anyting wrong here ?

rd.forward(request,response);
is just an simple call to forward method from your service method.
it is somthing like
public void myMethod() //service
{
// before calling fooo
foo(request,response)
// after calling foo
}
so after foo is done with its work it has to return control to myMethod.
hence you are getting those statements printed

hope this helps

Cheers
-Praful
[ October 25, 2004: Message edited by: Praful Thakare ]
 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Praful for your replies. But NO, getSession(false) should not be creating the session. The API and specification does say that. What am I reading wrong ? Also, if that is the case...how do I get the user do sign in...if the refresh does the job of gaining access. Agreed, I can use the userID password to control access...but still am not convinced.

As for the RequestDispatcher, according to the specs, forward gives the control to the 'other' web resource (something like 'go to') whereas include is something like a 'call' where the control gets back. It doesnt quite fit the behaviour.

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!