Servlet Key Points

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值