• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSession and servlets

 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Servlet Specification explains all.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am waiting too
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic