• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlets - stateless or stateful

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)...
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic