Hi ALL I just starting to learn JSP/Servlet. Can anybody tell me how single servlet instance is use to handle multiple requests? Is servlet implementation is example of Singleton pattern? Thank You Surender Singh [ April 28, 2008: Message edited by: Surender Singh ]
Hi, Welcome onboard. As far as comparison of servlet to singleton pattern is concerned, yes it is true, servlet is initialized only once when it is loaded by the servlet container. But there are differences- Further requests to the servlets are executed in a seperate thread where service() method is executed.
But the servlet may be loaded again by servlet container during life cycle of servlet.
According to the specification, a servlet is destroyed when "the servlet container determines that a servlet should be removed from service" (Servlet 2.3, section 2.3.4). Essentially, the Web container can potentially destroy and then reload and reinitialize the servlet at another time (as needed). In effect, startup code placed in an init() method can potentially be run multiple times during the life of an application.