| Author |
servlets - stateless or stateful
|
Shashank Rudra
Ranch Hand
Joined: Mar 26, 2009
Posts: 131
|
|
Hi everyone
Is there any simple answer to this question - "are servlets stateful or stateless?" .
IMHO - "they are stateless similar to underlying HTTP protocol. However it provides support for session objects to maintain the state in a web application"
|
Programmer Analyst || J2EE web development/design
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
If you are comparing them to EJBs, then they are completely different. There is one instance of a servlet in a container. So if you set a field in a servlet, it will be available to all the requests of that servlet. Example
This count field will be the same for all the clients (this technique might be used to find the number of hits to a servlet)...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
M K Rayapudi
Ranch Hand
Joined: Feb 19, 2007
Posts: 157
|
|
Shashank Rudra wrote:
Is there any simple answer to this question - "are servlets stateful or stateless?" .
That purely depends on your Servlet, i.e.
For e.g.if your servlet extends HttpServlet / SIPServlet, then your servlet is stateless, because HTTP/SIP are stateless protocols.
If your Servlet extends any stateful protocol (like RTSP, of course implementation may not be there) then your servlet becomes Stateful servlet.
|
R6i
|
 |
M K Rayapudi
Ranch Hand
Joined: Feb 19, 2007
Posts: 157
|
|
Ankit Garg wrote:
If you are comparing them to EJBs, then they are completely different. There is one instance of a servlet in a container. So if you set a field in a servlet, it will be available to all the requests of that servlet. Example
As Ankit said,
In other way that depends on your programming, you can program it in such a way to make your servlet to behave as stateless or stateful.
|
 |
Shashank Rudra
Ranch Hand
Joined: Mar 26, 2009
Posts: 131
|
|
Thanks Murali and Ankit for your comments. Now I understand that we can make a servlet (extending HTTPServlet) sessionAware by ways which are there. But by default will they be stateful? I think they will be not.
Again the instance variable retention has nothing to do with session and statefulness. That is just the scope of the instance variable which is due to multithreading approach we generally take in web application as far as servlet invocation is concerned.
|
 |
 |
|
|
subject: servlets - stateless or stateful
|
|
|