1

By setting <load-on-startup>-1</load-on-startup> property in web.xml we make servlet to load whenever server starts up.

I know pre initialized servlets are faster for first request.

My question is which kind of servlets are good member for this setting. Specifically for which kind of functionality it is useful?

Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62

3 Answers3

1

Loading on startup is a good practice for any servlet that performs any sort of time consuming operation. For example, a servlet that needs to establish a connection to a database.

As you have indicated, the servlet will then be able to respond to its first request faster - because it will already be initialized.

EJK
  • 12,332
  • 3
  • 38
  • 55
  • Establishing a database connection in `init()` method so that the `doXxx()` methods use it? That's really fishy. Food for read: [Is a static/applicationwide DB connection safe?](http://stackoverflow.com/a/9431863) – BalusC Jul 18 '15 at 09:32
1

The load-on-startup basically controls when the servlet's init() method is called.

So, logically, if you've a servlet with an init() method which does expensive and time consuming stuff, such as parsing XML configuration files and/or populating some application scoped data from some database, then it may be a good idea to do it on startup instead of on first request.

If you don't even have an init() method, then you don't need to care about load-on-startup.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

As you know well, servlet is loaded at first request. That means it consumes more time at first request. If you specify the load-on-startup in web.xml, servlet will be loaded at project deployment time or server start. So, it will take less time for responding to first request.

So if you have any servlet which takes lot of time during initialization ie. connecting to database, reading files etc. Better to specify load on startup for these kinds of servlet, so that they can be initialized during application startup. Thus making first request to be faster.

The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive 128 integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.

Prateek Kapoor
  • 947
  • 9
  • 18