Servlet 的生命周期分为init, service, and destroy.三部分。 The init( ) Method The init method is called by the servlet container after the servlet class has been instantiated. The servlet container calls this method exactly once to indicate to the servlet that the servlet is being placed into service. The init method must complete successfully before the servlet can receive any requests. You can override this method to write initialization code that needs to run only once, such as loading a database driver, initializing values, and so on. In other cases, you normally leave this method blank. The signature of this method is as follows: public void init(ServletConfig config) throws ServletException The service( ) Method The service method is called by the servlet container after the servlet's init method to allow the servlet to respond to a request. This method has the following signature: public void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException The destroy( ) Method The servlet container calls the destroy method before removing a servlet instance from service. This normally happens when the servlet container is shut down or the servlet container needs some free memory. public void destroy()
Servlet 的生命周期
最新推荐文章于 2023-05-10 18:35:59 发布