Configuring servlet initialization in the DD

You already know that servlets, by default, are initialized at first request. That means the first client suffers the pain of class loading, instantiation, and initialization (setting a ServletContext, invoking listeners, etc.), before the Container can do what it normally does— allocate a thread and invoke the servlet’s service() method.

If you want servlets to be loaded at deploy time (or at server restart time) rather than on first request, use the <load-on-startup> element in the DD. Any non-negative value for <load-on-startup> tells the Container to initialize the servlet when the app is deployed (or any time the server restarts).

If you have multiple servlets that you want preloaded, and you want to control the order in which they’re initialized, the value of <load-on-startup> determines the order! In other words, any non-negative value means load early, but the order in which servlets are loaded is based on the value of the different <load-on-startup> elements.

image with no caption

In the DD

image with no caption

Q:

Q: Wouldn’t you ALWAYS want to do this? Shouldn’t everyone just use <load-on-startup>1</load-on-startup> by default?

A:

A: To answer that question, you ask yourself, “How many servlets do I have in my app, and how likely is it that they’ll all be used?” And you’ll also need to ask, “How long does it take each servlet to load?” Some servlets are rarely used, so you might want to conserve resources by not loading the rarely-used servlets in advance. But some servlets take so painfully long to initialize (like the Struts ActionServlet), that you don’t want even a single client to experience that much latency. So, only you can decide, and you’ll probably decide on a servlet-by-servlet basis, evaluating both the pain level and likelihood of use for each servlet.

Note

Values greater than one do not affect the number of servlet instances!

The value you use: <load-on-startup>4</load-on-startup> does NOT mean “load four instances of the servlet”. It means that this servlet should be loaded only AFTER servlets with a <load-on-startup> number less than four are loaded. And what if there’s more than one servlet with a <load-on-startup> of 4?

The Container may choose the order of loading of servlets with the same load-on-startup value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset