Forums Register Login

JWeb+ Question ID :996245849311

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 684 times.
Similar Threads
context and static parameters
More than one servlet per web application
Cross Context sharing of Objects
Sharing Objects between Webapps
How can a non-servlet find application root and best way to access a servlets data?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:55:37.