This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes thread is logically equal to a context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "thread is logically equal to a context" Watch "thread is logically equal to a context" New topic
Author

thread is logically equal to a context

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
I develop a web application,deployed some POJO(Business Object) in server,Please confirm whether my understanding correct?

1)When many users access the same business object then server server create several logical or virtual contexts ,in each context all business objects exist,if property of a business object is changed in one context it never change the value of the property in another context.

if many user access server then it do multithreading,
the same thread is used for the user still he access the server
[ October 02, 2008: Message edited by: jacob deiter ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
If I understand you correctly, the answer is no.

The servlet container does not perform magic tricks by itself. What it does provide are:

1. Session mechanism - the session context is attached to a given session ID. Assuming you manage sessions correctly, one session per user.
2. ServletContext - one ServletContext per web application shared by all users.
3. Instance variables - since there is only one instance of a given servlet, all instance variables are shared by all users of that servlet.
4. Request context - each request operates in its own Thread having its own ServletRequest and ServletResponse object seen by no other Thread.

Bill
Jack Doson
Greenhorn

Joined: Oct 02, 2008
Posts: 9
For each request Server will create a new Thread of servlet being requested (or Jsp(but not pojos)) and that thread will instantiate the required pojo(to help it in processing request).. actual pojo will not suffer with any kind of Multithreading..!

--->thing that suffer with multithreading are only instance variables and static variables with in any servlet or jsp..!(Pojo and even beans also never suffer with multithreading..!)


Jack Dososn..!
Jack Doson
Greenhorn

Joined: Oct 02, 2008
Posts: 9
and with in any application there is going to be only one instance of Servlet or Jsp...!
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
actual pojo will not suffer with any kind of Multithreading..!


Hi Jack

please clarify the below also

I read in books that main feature of servlet is multithreading ,for example if I call java object and do some operation inside of servlet's service method(dopost or doge).so if servlet is multithreaded then java object also be multihtreaded or not ? because java object in sevlet's service method
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

so if servlet is multithreaded then java object also be multihtreaded or not ? because java object in sevlet's service method


The servlet is multithreaded means separate threads are allocated to separate requests.
And I dont understand what do you mean by object should be multithreaded. Objects are not multithreaded, the code that accesses them is.


Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I feel there is a lot of advice of dubious quality being offered here.

Firstly, all Java code is able to be executed by multiple threads, how it manages the threading and what it does under the influence of those threads is the point to consider.

Any shared mutable state running under multiple threads must have its state protected.

Servlet containers manage threads, and the creation, allocation and management of threads is completely up to the container. It is not related to contexts, sessions, users or anything else. (with the exception of William's comments above)

Servlets by nature, are designed to be accessed by multiple threads. If they share their state or access and other shared state, then those objects need to consider their behaviour under multiple threads.

I recommend "Java Concurrency in Action" if you want to get it right.
[ October 03, 2008: Message edited by: David O'Meara ]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816


David O'Meara :
I recommend "Java Concurrency in Action" if you want to get it right


Did you mean "Java Concurrency in Practice" ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

That one too
(I'm reading it at the moment though)
sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

let me explain with a practical example..

a context is a party..
context initialized== party started.
there are seperate groups/stacks of methods enjoying party.
so each group/stack makes a seperate thread.


Sudheer
SCWCD, SCJP 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread is logically equal to a context
 
Similar Threads
How to save a file inside an application context?
session in servers
new instance per user
Newbie question on state management
Thread in JEE