• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JWeb+ Question ID :996245849311

 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In the "General Comments" of this question, it says "A session is automatically shared accross multiple servlets of same webapp. So, attributes set by one servlet can be accessed by other servlets of same webapp."
If this were the case, what difference does it make with Context objects?
Is Request objects can also be shared among servlets of the same webapp?
Thank you very much!
 
David Gu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A session obj can only be seen by that specific client. That means it can not be seen by other clients.
A servlet Context object can be seen by no only this specific client. It can be seen by all the clients.
David
SCJP/SCWCD/SCJA
 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for curiosity, if a session object is unique to a client, thus cannot be seen by other clients, and assume that a client is running one servlet instance at a particular point in time, why do we say session objects are not thread safe?
Similarly, how come request objects are not thread safe, if they can only be accessed by one client on a sigle request?
Again thank you very much for the help
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marlon tan:
assume that a client is running one servlet instance at a particular point in time

This is where your logic breaks down. Someone can use "File New" or [Alt][N] and get a second browser with the same sessionID and hit the server at the exact same time for a different servlet. Additionally you can place an Applet on the page and communicate back to the server using that session for monitoring. One you control (the applet) and one you cannot (new browser).

------------------
I Hope This Helps
Carl Trusiak, SCJP2
 
David Gu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marlon,
Session obj is not thread safe:
You may launch two brwosers, and activate two thread of the same servelt1, these two servlet threads can access the same sessionObj. On the other hand,you may have servlet1 and servlet2 access the same sessionObj.
Request Obj is thread safe, as it exists only per request basis. No one else can access it, even you launch two browser to access the same page/servlet, the request obj are different.
David
SCJP/SCWCD/SCEJA

 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Thank you for the reply.
Given these situations, there's no way we can make our sevlets thread-safe, such that it will protect our Session resource. For example, we create a new browser which will launch another thread that will access same servlet, Servlet1. Even if Servlet1 implements SingleThreadedModel, the servlet container will still instantiate another instance of Servlet1. Still, both instances might access the session object at the same time, even if it was accessed within the service() method.
Am I correct with my conclusion?
Thank you very much.
- Marlon Tan
 
David Gu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Morlan,
You are correct.
Multiple servlets executing request threads may have active access to a single session object at the same time.
The DEVELOPER has the responsibility for
synchronizing access to session resources as appropriate.
This is the key point which Sun will test you.!!
David
SCJP/SCWD/SCEJA
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic