Hello!
Can some one explain if there is a difference in:
1. Separate instance per each request.
2. Separate thread with the same instance per each request.
I am about http request and servlet.
Thank you!
Separate object per request means a separate object of something is created per request. So each request has its own object, and hence its own copy of member variables. Like for instance in
struts, when we code action classes, each request gets its own object of the action class.
Separate threads that share an object means separate threads have the same object reference and hence they share the object's data. Like for instance a servlet is instantiated only once. When a new request comes in, another thread is created to cater to it.
All these different threads ( in the case of servlets ) share the same object and its data. This is one reason why we don't create member variables in servlets ( unless we don't care about accessing them in a thread unsafe way). What we instead do is create request and session attributes which are unique per request and session respectively. This is different from the action classes of struts where each request gets its own action class object.
Disclaimer - I'm fairly new to these topics. Have recently started learning them ( about two days back or so ). But this is what I've found out so far.