This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton servlet

 
soven rout
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to implement singleton concept in servlet?
 
Tim Moores
Bartender
Posts: 2500
10
  • 2
  • Mark post as helpful
  • send pies
  • 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?

  •  
    Devaka Cooray
    ExamLab Creator
    Marshal
    Pie
    Posts: 4119
    195
    Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
    • 4
    • Mark post as helpful
    • send pies
    • 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.
     
    jagrutkumar dalwadi
    Ranch Hand
    Posts: 83
    Hibernate Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • 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).
     
    Palak Mathur
    Ranch Hand
    Posts: 322
    IntelliJ IDE Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • 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?
     
    Swetha Bhagavathula
    Ranch Hand
    Posts: 112
    Java Oracle
    • 2
    • Mark post as helpful
    • send pies
    • 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 .
     
    Tim Holloway
    Saloon Keeper
    Pie
    Posts: 17633
    39
    Android Eclipse IDE Linux
    • 3
    • Mark post as helpful
    • send pies
    • 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.
     
    Prabhakar Reddy Bokka
    Ranch Hand
    Posts: 196
    Java Oracle Spring
    • 1
    • Mark post as helpful
    • send pies
    • 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: 322
    IntelliJ IDE Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • 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
    Pie
    Posts: 17633
    39
    Android Eclipse IDE Linux
    • 1
    • Mark post as helpful
    • send pies
    • 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: 322
    IntelliJ IDE Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • 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!!
     
    Piyush Mangal
    Ranch Hand
    Posts: 196
    • 0
    • Mark post as helpful
    • send pies
    • 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: 322
    IntelliJ IDE Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • 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
    • 1
    • Mark post as helpful
    • send pies
    • 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
    Pie
    Posts: 17633
    39
    Android Eclipse IDE Linux
    • 1
    • Mark post as helpful
    • send pies
    • 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
    Java Oracle
    • 0
    • Mark post as helpful
    • send pies
    • 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
    • 0
    • Mark post as helpful
    • send pies
    • 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
    Java Oracle
    • 0
    • Mark post as helpful
    • send pies
    • 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
    • 0
    • Mark post as helpful
    • send pies
    • 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.
     
    Nauman Hasan
    Ranch Hand
    Posts: 34
    • 0
    • Mark post as helpful
    • send pies
    • 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
     
    Pat Farrell
    Rancher
    Posts: 4660
    5
    Linux Mac OS X VI Editor
    • 0
    • Mark post as helpful
    • send pies
    • 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
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic