Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets - stateless or stateful

 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
M K Rayapudi
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
M K Rayapudi
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramesh vanka
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class rameshservlet extends HttpServlet{
}

raemshservlet does not have any state means, its not having the instance variable and static variable, any object state depends on the instance variables and static variables.

People get doubt on rameshservlet extends HttpServlet, HttpServlet having state variables ?
HttpServlet is a abstract class and also its having instance and statice variables are private

public abstract class More ...HttpServlet extends GenericServlet
implements java.io.Serializable
{
private static final String METHOD_DELETE = "DELETE";
private static final String METHOD_HEAD = "HEAD";
...
}

rameshservlet cant inherited those static variables because they are private.

rameshservlet is stateless.

Ex:2 class nitservlet extends httpservlet{
private int count = 0;
}

nitservlet having the instance variable, so that nitservlet having the state.


Hope, you understand to make the servlet as a stateless or stateful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic