Servlet logo
Servlet Tutorial

Overview  
HTTP/CGI  
Servlets  
JSP  
Resources  

Zameer's Education


 

Introduction | Servlets & HTTP | Life-Cycle | Sessions | Summary | Examples

Life Cycle: Loading | Servicing Requests | Threading | Reloading

Servlet Reloading

A servlet will be reloaded if its class file changes on disk and that class file is kept under the assigned servlet directory for that servlet engine. Every time a service request comes in for a servlet kept in the servlet directory, it's timestamp is checked and if it has been modified, it is reloaded. Servlet reloading is a useful programming aid that vastly reduces the edit/compile/test development cycle for servlets because there is no need to stop and start the web server.

When you have finished testing your servlet, it should be taken out of the servlets directory and placed on the servlet engines CLASSPATH. In Java, anything on the CLASSPATH is loaded by the Primordial ClassLoader and loaded once. It is for this reason that the servlet directory is not on the servlet engine's CLASSPATH. The servlet directory is kept off the CLASSPATH so that servlet classes can be reloaded as and when they change, where classes on the CLASSPATH are loaded only once. For servlets in development, the servlet directory provides a pretty useful debug cycle, whilst for those in production, being on the CLASSPATH means that the servlet engine does not need to check their timestamps for every single service requests.

Finally, it is recommended that you place shared classes on the CLASSPATH and not in the servlet directory. A shared class is one that is referenced or its instances used by multiple servlets and/or multiple instances of the same servlet. If a shared class resides in the servlet directory it is eligible for reloading. For example, if a servlet A references a shared class B, if A is reloaded it is highly likely that B will be reloaded as well. This gives rise to a number of scenarios where there can be multiple versions of the shared class B being referenced by multiple servlets and/or servlet instances. This leads to undefined behaviour within the servlet engine. To prevent shared classes from being reloaded, place shared classes on the CLASSPATH. Use Table 1 to determine whether your classes should be considered as shared.

Shared Classes

A class that is used in more that one servlet. These are typically support classes and/or beans that provide common behaviour such as utility classes for accessing the database, reading files from the disk, and so on, that are shared between multiple servlets.

Static Variables

Static variables are at the class level and are shared between all multiple servlet instances. Remember multiple servlet instances in a servlet engine occur within the SingleThreadModel and for serlvets registered under more than one name.

HttpSession Value Classes

Classes that are stored within HttpSession objects are shared between multiple servlets and/or multiple servlet instances. It is important that the class instance for objects contained within an HttpSession is not prone to change.