I have now created a java servlet program,the servlet class which extends HttpServlet is named com.servlet.Main. As we all know , every servlet class has two functions: doGet() and doPost(),one for http get request and the other for http post request. My question is,JVM will create a new com.servlet.Main instance for each coming request or just maintain a singleton instance for all requests?
            Asked
            
        
        
            Active
            
        
            Viewed 606 times
        
    2 Answers
0
            
            
        A Servlet container will only ever create one instance of your Servlet implementation for each declaration in the deployment descriptor. This is not a true singleton, only effectively a singleton relative to the ServletContext. Nothing prevents you from creating more instances.
Note, that the entry point of all Servlet applications is the Servlet#service(ServletRequest, ServletResponse) method. HttpServlet, an sub type of Servlet, implements this method to delegate to a number of methods which custom implementations should override. These are doGet, doPost, doPut, doDelete, doHead, etc.
 
    
    
        Sotirios Delimanolis
        
- 274,122
- 60
- 696
- 724
0
            
            
        The servletcontainer reuses the same servlet instance for every request.
See this stackoverflow post
 
    
    
        Community
        
- 1
- 1
 
    
    
        Georgios Syngouroglou
        
- 18,813
- 9
- 90
- 92
