aspose file tools*
The moose likes Servlets and the fly likes Singleton servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Singleton servlet" Watch "Singleton servlet" New topic
Author

Singleton servlet

soven rout
Greenhorn

Joined: Feb 19, 2012
Posts: 19
How to implement singleton concept in servlet?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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?

  • Devaka Cooray
    ExamLab Creator
    Saloon Keeper

    Joined: Jul 29, 2008
    Posts: 3268
        
      52

    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.


    Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
    What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
    jagrutkumar dalwadi
    Ranch Hand

    Joined: Mar 26, 2009
    Posts: 83

    We can make servlet singleton. But we can make it as Singel threaded(Not recommoneded) or multi threaded(by default).
    Palak Mathur
    Ranch Hand

    Joined: Jan 29, 2007
    Posts: 313

    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?


    Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
    Swetha Bhagavathula
    Ranch Hand

    Joined: Jan 04, 2011
    Posts: 112

    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 .


    SCJP5
    Tim Holloway
    Saloon Keeper

    Joined: Jun 25, 2001
    Posts: 16305
        
      21

    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.


    Customer surveys are for companies who didn't pay proper attention to begin with.
    Prabhakar Reddy Bokka
    Ranch Hand

    Joined: Jul 26, 2005
    Posts: 193

    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.

    SCJP 5, SCWCD 5
    Palak Mathur
    Ranch Hand

    Joined: Jan 29, 2007
    Posts: 313

    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

    Joined: Jun 25, 2001
    Posts: 16305
        
      21

    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

    Joined: Jan 29, 2007
    Posts: 313

    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!!
    Piyush Mangal
    Ranch Hand

    Joined: Jan 22, 2007
    Posts: 196
    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

    Joined: Jan 29, 2007
    Posts: 313

    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

    Joined: Jan 22, 2007
    Posts: 196
    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

    Joined: Jun 25, 2001
    Posts: 16305
        
      21

    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

    Joined: Jan 04, 2011
    Posts: 112

    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

    Joined: Jan 22, 2007
    Posts: 196
    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

    Joined: Jan 04, 2011
    Posts: 112

    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

    Joined: Feb 19, 2012
    Posts: 19
    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.
    Nauman Hasan
    Ranch Hand

    Joined: Jul 27, 2005
    Posts: 34
    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
    Pat Farrell
    Rancher

    Joined: Aug 11, 2007
    Posts: 4659
        
        5

    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Singleton servlet