Servlet 的生命周期

来源:互联网 发布:淘宝商城的收费标准 编辑:程序博客网 时间:2024/04/20 04:22
Servlet 的生命周期分为init, service, and destroy.三部分。The init( ) MethodThe 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 ServletExceptionThe service( ) MethodThe 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.IOExceptionThe destroy( ) MethodThe 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()