wood burning stoves 2.0*
The moose likes Servlets and the fly likes Related to Session and Request objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Related to Session and Request objects" Watch "Related to Session and Request objects" New topic
Author

Related to Session and Request objects

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 97
We fetch the session object from the request object.
Then what is the difference between request and session?
Why cant I use the request itself instead of session?
Is it that session can be fetched from request object only? Or is there any other way to get the session object?
Somebody please explain.

Regards,
Sriram
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hi,

yes, you are right session retrieval from request object. But you have to read about variable scope. The request and session are two different scope.

Request scope basically use for particular request. while session scope available through out web application. so which variable set as session scope you can access any part of request and therefor you have to access session scope form request object.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 97
Hi Nishan,

I am still not able to get the complete picture.
I am unable to get the exact meaning of the statement "while session scope available through out web application. so which variable set as session scope you can access any part of request and therefor you have to access session scope form request object." :-(

Can somebody help me out in understanding the session and request scope and also about session and request objects?

Regards,
Sriram

Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hi,

while session scope available through out web application. so which variable set as session scope you can access any part of request and therefor you have to access session scope form request object


what I mean to say, when you set any variable using session you can you can use this variable at any place of your web application. That means at any request of your web application. Session available for you and for your web application at any time and at any place. While when you use request.setAttribute("user", "Nishan"); then that user variable available only that particular request. After that request if you use that user attribute that scope of user lost and you can not access that variable. While instead of if you use session then after second request that user object available for you..

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Related to Session and Request objects
 
Similar Threads
Session Management Mechanism
Maintain Session with Thread Safe
Difference between a parameter and an attribute?
pass bean from jsp:setProperty to servlet?
scope