• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Singleton servlet

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to implement singleton concept in servlet?
 
Saloon Keeper
Posts: 7645
178
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few questions:

  • What, exactly, do you mean by that?


  • Why do you want to do it?


  • Why do you think it would be any different in a servlet than in any other class?

  •  
    Sheriff
    Posts: 7341
    1404
    IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
    • Likes 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Note that managing objects of servlets is a responsibility of the container - not something you should involve in. Attempting to instantiate a servlet is a good start to have many endless problems. You might be interested in Servlet Life-cycle management.
     
    Ranch Hand
    Posts: 83
    Hibernate Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    We can make servlet singleton. But we can make it as Singel threaded(Not recommoneded) or multi threaded(by default).
     
    Ranch Hand
    Posts: 343
    Mac OS X Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    soven rout wrote: How to implement singleton concept in servlet?



    What will you do by making a Servlet singleton? Are you trying to write a servlet container?
     
    Ranch Hand
    Posts: 112
    Oracle Java
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    soven rout wrote: How to implement singleton concept in servlet?



    hi.. though we write servlet program, we never and we cant create object to our servlet class because it is task that is done by the container. because request may come from anyclient at any time and handling all requests and sending respone is not a joke of course it's impossible manually.that is why web servers are developed with containers.they execute our servlet program(extending predefined servlets likeHttpServlet or GenericServlet) and perforform the instantiation and initialization events based upon the configurations done in web.xml.

    A container create another object to our servlet program if it gets max/many requests like above 150 etc at a time .which happens very rarely. Hence, we can say ,a servlet is a single instance multithread component. moreover the request and response objects in servive(-,0)/doXxx(-,-) methods are not the objects of javax.servlet.ServletRequest and javax.servlet.ServletResponse(interface) but are the objects of classes supplied by the underlying servlet container implemeting these interfaces ex:in tomcat server the response object classname is:"org.apache.catalina.connector.ResponseFacade",differs server to server .
     
    Saloon Keeper
    Posts: 28316
    207
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A servlet is not a "program". It is a component of a deployed Web Application that presents a service method, which (usually) routes an incoming URL request to a secondary method such as doGet() or doPost().

    The servlet operates when the container obtains a thread from the container's Thread Pool and runs it with a request to invoke the servlet's service() method. So, yes, servlets must be thread-safe as must any code that they invoke.

    Being "thread-safe", however, does not give one license to lock the servlet for long periods of time, nor may the servlet spawn secondary threads for long-running tasks. There are tricks to managing such things, but explaining them is for another time.
     
    Ranch Hand
    Posts: 198
    Oracle Spring Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    How to implement singleton concept in servlet?



    It is not singleton servlet but a single threaded servlet we can create by implementing a marker interface 'SingleThreadModel'.
    But this is deprecated in JavaEE 5.
     
    Palak Mathur
    Ranch Hand
    Posts: 343
    Mac OS X Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Prabhakar Reddy Bokka wrote:

    How to implement singleton concept in servlet?



    It is not singleton servlet but a single threaded servlet we can create by implementing a marker interface 'SingleThreadModel'.
    But this is deprecated in JavaEE 5.



    Will SingleThreadModel internally be using singleton pattern?
     
    Tim Holloway
    Saloon Keeper
    Posts: 28316
    207
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Palak Mathur wrote:

    Prabhakar Reddy Bokka wrote:

    How to implement singleton concept in servlet?



    It is not singleton servlet but a single threaded servlet we can create by implementing a marker interface 'SingleThreadModel'.
    But this is deprecated in JavaEE 5.



    Will SingleThreadModel internally be using singleton pattern?



    Not literally. What it does is it instructs the container not to schedule more than one thread to use the servlet at a time. Until the first thread returns, no other thread may be invoked on it.
     
    Palak Mathur
    Ranch Hand
    Posts: 343
    Mac OS X Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tim Holloway wrote:

    Palak Mathur wrote:

    Prabhakar Reddy Bokka wrote:

    How to implement singleton concept in servlet?



    It is not singleton servlet but a single threaded servlet we can create by implementing a marker interface 'SingleThreadModel'.
    But this is deprecated in JavaEE 5.



    Will SingleThreadModel internally be using singleton pattern?



    Not literally. What it does is it instructs the container not to schedule more than one thread to use the servlet at a time. Until the first thread returns, no other thread may be invoked on it.



    Ok!! Thank you Tim!!
     
    Ranch Hand
    Posts: 196
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There can only be a single instance of a servlet at any given point of time. All concurrent requests are processed by a sever by having multiple threads executing on a single instance of a servlet. So in a way, your servlet is already singleton.
     
    Palak Mathur
    Ranch Hand
    Posts: 343
    Mac OS X Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piyush Mangal wrote:There can only be a single instance of a servlet at any given point of time. All concurrent requests are processed by a sever by having multiple threads executing on a single instance of a servlet. So in a way, your servlet is already singleton.



    Are you sure? Is it multiple thread on single instance or the servlet container creates a pool of instances for a servlet to handle concurrent responses?
     
    Piyush Mangal
    Ranch Hand
    Posts: 196
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Palak Mathur wrote:

    Piyush Mangal wrote:There can only be a single instance of a servlet at any given point of time. All concurrent requests are processed by a sever by having multiple threads executing on a single instance of a servlet. So in a way, your servlet is already singleton.



    Are you sure? Is it multiple thread on single instance or the servlet container creates a pool of instances for a servlet to handle concurrent responses?



    Yes, We are talking about Servlet not StatelessSessionBean here. Moreover A container might load or unload a given servlet based on the load on the server but there would be only single instance of the servlet.
     
    Tim Holloway
    Saloon Keeper
    Posts: 28316
    207
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Originally, I think that multiple instances were possible. However, in practice, I never saw it done and I think it may even be formally forbidden by current specs.
     
    Swetha Bhagavathula
    Ranch Hand
    Posts: 112
    Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    suppose if any websites (web application which is hosted in internet) and if it gets multiple requests at a time will the container creates multiple instances to the servlet component ?(if that web applcaition is developed using servlets as web resource program
    ). for example just assume xxx bank site getting lots of requests at a time..?
     
    Piyush Mangal
    Ranch Hand
    Posts: 196
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    suppose if any websites (web application which is hosted in internet) and if it gets multiple requests at a time will the container creates multiple instances to the servlet component ?(if that web applcaition is developed using servlets as web resource program
    ). for example just assume xxx bank site getting lots of requests at a time..?



    That is where clustering and load balancing comes into picture. You deploy multiple instances of web server and load balancer manages the load by dispatching the request to the appropriate server.
    Even databases are also clustered to achieve the desired scalability.
     
    Swetha Bhagavathula
    Ranch Hand
    Posts: 112
    Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piyush Mangal wrote:

    suppose if any websites (web application which is hosted in internet) and if it gets multiple requests at a time will the container creates multiple instances to the servlet component ?(if that web applcaition is developed using servlets as web resource program
    ). for example just assume xxx bank site getting lots of requests at a time..?



    That is where clustering and load balancing comes into picture. You deploy multiple instances of web server and load balancer manages the load by dispatching the request to the appropriate server.
    Even databases are also clustered to achieve the desired scalability.




    thank you but sorry at the same time as I am beginner of servlets still i didn't go through the concepts of cluster and load balancing etc.. so please excuse me for that and i would be happy if you explain in brief ..
     
    soven rout
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    A servlet is not a "program". It is a component of a deployed Web Application that presents a service method, which (usually) routes an incoming URL request to a secondary method such as doGet() or doPost().

    The servlet operates when the container obtains a thread from the container's Thread Pool and runs it with a request to invoke the servlet's service() method. So, yes, servlets must be thread-safe as must any code that they invoke.

    Being "thread-safe", however, does not give one license to lock the servlet for long periods of time, nor may the servlet spawn secondary threads for long-running tasks. There are tricks to managing such things, but explaining them is for another time.


    thanks Tim Holloway for your concern .I'm a greenhorn and these things helped me a lot.
     
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piyush Mangal wrote:

    suppose if any websites (web application which is hosted in internet) and if it gets multiple requests at a time will the container creates multiple instances to the servlet component ?(if that web applcaition is developed using servlets as web resource program
    ). for example just assume xxx bank site getting lots of requests at a time..?



    That is where clustering and load balancing comes into picture. You deploy multiple instances of web server and load balancer manages the load by dispatching the request to the appropriate server.
    Even databases are also clustered to achieve the desired scalability.



    Normally, Servlets are multi-threaded so if the server gets multiple requests it will simply use the same Servlet instance to serve all the requests concurrently.

    Clustering and load balancing can achieve other non-functional goals however are not needed just to serve multiple requests for the same Servlet.

    ~Nauman
     
    Rancher
    Posts: 4804
    7
    Mac OS X VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Singletons considered harmful.

    The Singleton pattern is very popular, and used far more often then it is properly justified.
    In general, if you think your problem needs a singleton, you are asking the wrong questions
     
    The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic