Servlet Reloading
If you tried using these counters for yourself, you may have noticed
that any time you recompiled one, its count automatically began again
at 1. Trust us—it’s not a bug, it’s a feature. Most
servers automatically reload a
servlet after its class file (under
the default servlet directory, such as
server_root
/servlets)
changes. It’s an on-the-fly upgrade procedure that greatly
speeds up the development-test cycle—and allows for long server
uptimes.
Servlet reloading may appear to be a simple feature, but it’s
actually quite a trick—and requires quite a hack.
ClassLoader objects are designed to load a class
just once. To get around this limitation and load servlets again and
again, servers use
custom class loaders that load
servlets from the default servlets directory. This explains why the
servlet classes are found in
server_root
/servlets,
even though that directory doesn’t appear in the server’s
classpath.
When a server dispatches a request to a servlet, it first checks if the servlet’s class file has changed on disk. If it has changed, the server abandons the class loader used to load the old version and creates a new instance of the custom class loader to load the new version. Old servlet versions can stay in memory indefinitely (and, in fact, other classes can still hold references to the old servlet instances, causing odd side effects, as explained in Chapter 11), but the old versions are not used to handle any more requests.
Servlet reloading is not performed ...