Execute servlet on startup of the application

后端 未结 3 1698
野的像风
野的像风 2021-01-15 11:28

I build a web application with JSPs and in my servlet I have:

public class MyServlet extends HttpServlet {
    protected void processRequest(HttpServletReque         


        
3条回答
  •  天命终不由人
    2021-01-15 12:12

    In my point of view, a good way is to implement a Servlet Context Listener. It listens to application startup and shutdown.

    public class YourListener implements javax.servlet.ServletContextListener {
    
        public void contextInitialized(ServletContextEvent sce) {
        }
    
        public void contextDestroyed(ServletContextEvent sce) {
        }
    }
    

    And then, you configure the listener in your web.xml () or with the @WebServletContextListener annotation.

提交回复
热议问题