File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HttpSession and servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HttpSession and servlets" Watch "HttpSession and servlets" New topic
Author

HttpSession and servlets

Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
suppose a servletA starts a session for a particular client....now if the client accesses a servletB..can the servletB on the next request use the session that was created by the servletA?

how does a httpsession work?


Do What You Wanna Be....Taking Things The Way They Come
scjp 1.6 91 percent, scmad 90 percent(rounded off to nearest integer)
Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
am i right that clientA once has started a session within an application....this session can be used by any of the servlets in the application as far as the same client is there...for a clientB there will be different session....so sessions are attached to clients....but only to the same application
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

The Servlet Specification explains all.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

ted mosby wrote:.so sessions are attached to clients....but only to the same application

Yes, basically when a client's session starts, the server sends a cookie to the client which contains the session ID of the client. With that cookie the server identifies that client on the next request (I'm skipping the part where cookies are disabled for simplicity)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
Ankit Garg wrote:
ted mosby wrote:.so sessions are attached to clients....but only to the same application

Yes, basically when a client's session starts, the server sends a cookie to the client which contains the session ID of the client. With that cookie the server identifies that client on the next request (I'm skipping the part where cookies are disabled for simplicity)...


Wait! So you guys are saying that if ServletA starts a SessionA using cookie ID or sessionID then SessionA can be used by other ServletB?

I thought it otherwise!
I thought that ...Lets say ClientA through webContainer (webserver) invokes serveltA.....and hence SessionA.
ClientB throught same webconatiner can invoke servletA but will get sessionB.
SessionA and SessionB must be different so that they dont mix.

However, what happens if another servlet (ServletB) comes into play? I thought servletB should start another sessionC. However, this looks dubious!! SCWCD is confusing


Working on my SCWCD so I can be a J2EE consultant earning millions of dollars and showing everyone I can
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
However, what happens if another servlet (ServletB) comes into play? I thought servletB should start another sessionC.

If ServletB will start a new session sessionC then how can it treat clientA which is using sessionA as clientA. Since sessionA != sessionC.


SCJP 1.5(97%) My Blog
Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
shivendra tripathi wrote:
However, what happens if another servlet (ServletB) comes into play? I thought servletB should start another sessionC.

If ServletB will start a new session sessionC then how can it treat clientA which is using sessionA as clientA. Since sessionA != sessionC.


Now I have two options. Either it will use the same sessionA using the sessionID or cookieID or will start a new sessionC . Please help. I am confused. ACtually I am studying the session chapter right now . Please please
Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
in the hfsj one point is given....same servlet....different client....different request....different session....have you read about the session scope? it is in the scope till the session is alive and is associated with the client....context scope>session scope>request scope. when the application dies the context object dies. but only when the session lives as long as specified in the DD or till it is invalidated....maybe i am missing something here....till the client is the same...same session can be used through out the application whether it is the servletA or the servletB
do correct if you feel so....
Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
Ankur Jain Kothari wrote:in the hfsj one point is given....same servlet....different client....different request....different session....have you read about the session scope? it is in the scope till the session is alive and is associated with the client....context scope>session scope>request scope. when the application dies the context object dies. but only when the session lives as long as specified in the DD or till it is invalidated....maybe i am missing something here....till the client is the same...same session can be used through out the application whether it is the servletA or the servletB
do correct if you feel so....


If ClientA invoking ServletA ...hence sessionA, then how is clientA invoking servletB? If clientA has to invoke servletB, the does he not become clientB? ??? It might be the same machine (IP), but since he is invoking another instance does he not become clientB? clientA stuffs may still be running

It is like if you clientA open amazon.com and doing some stuff. before finishing you open another webbrowser and go to amazon.com and do some other stuff. It is the same person.. ... but to the webcontainer aren't these different clients.. hence different servlets...hence different sessions?
Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
maybe he invokes servletB through forwarding....if he had to start a new servletB he does it through another browser which starts new session.....session can be retrieved from the request...wherever the request obejct is same...the same session can be used....whatever i wrote is all maybe....even am studying.
Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
Ankur Jain Kothari wrote:maybe he invokes servletB through forwarding....if he had to start a new servletB he does it through another browser which starts new session.....session can be retrieved from the request...wherever the request obejct is same...the same session can be used....whatever i wrote is all maybe....even am studying.


hmmm.....You are right. You can use forwarding to invoke another servlet that means.. Yes! same session can be used. However, let us see if any gurus here give any response
Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
am waiting too
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Swagato Bhatta wrote:
Now I have two options. Either it will use the same sessionA using the sessionID or cookieID or will start a new sessionC . Please help. I am confused. ACtually I am studying the session chapter right now . Please please
It will use same session if request send by client has any otherwise create a new session and send it back(while sending response) to client for subsequent request.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpSession and servlets