Servlet Key Points

来源:互联网 发布:淘宝代刷远程单安全吗 编辑:程序博客网 时间:2024/04/28 21:11

init() method

The lifecycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
- (1) If an instance of the servlet does not exist, the web container.
- a. Loads the servlet class.
- b. Creates an instance of the servlet class.
- c. Initialises the servlet instance by calling the init method.
- (2) Invokes the service method, passing request and response objects.

destroy() method

The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.

Called by the servlet container (when server is shutdown) to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet’s service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

How to load Servlet on server(tomcat) start-up

In web.xml file add:

<servlet>...<loadon-startup>1</loadon-startup>...</servlet>

Modifying servlet file during server running

Server will recompile servlet java file to .class file and reload. The container (server) will first destroy() all inited servlet and reload(compile). Constructor, init() still will be executed at the first time user issue request if not specified in web.xml.

0 0
原创粉丝点击