简体   繁体   中英

When exactly a web container initialize a servlet?

When exactly a web container initialize a servlet? Is it guaranteed that it does it at web container start up time? Thanks.

No, it's not. First, the webapp itself is not guaranteed to be started when the container starts (that depends on the specific container configuration). And even if the webapp is started, the specification says:

Servlets are initialized either lazily at request processing time or eagerly during deployment. In the latter case, they are initialized in the order indicated by their load-on-startup elements.

When exactly a web container initialize a servlet?

Either at the time of loading the web application or on the first request to the servlet. This is configurable in web.xml using load-on-startup flag

Is it guaranteed that it does it at web container start up time?

Nothing is guranteed when it comes to the container. It depends how the contianer is written .The only way to request the container is through configurable param load-on-startup in in web.xml.

Depends on how you are defining and configuring your servlet.

You might find this clearly in docs

Initializing a Servlet After the web container loads and instantiates the servlet class and before it delivers requests from clients, the web container initializes the servlet. To customize this process to allow the servlet to read persistent configuration data, initialize resources, and perform any other one-time activities, you override the init method of the Servlet interface. A servlet that cannot complete its initialization process should throw UnavailableException.

If you want to participate in that process ovveride init method of the Servlet interface and do the required stuff there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM