[ Team LiB ] Previous Section Next Section

Reloading Other Classes

When you change a JSP, the JSP engine automatically reloads it the next time a browser requests it. Some servlet engines even reload a servlet when it changes. Many engines, however, do not automatically reload associated classes. In other words, when you create a Java class that is a companion to a JSP and you change that companion class, it isn't reloaded even if the JSP itself is reloaded.

For example, the JSP in Listing 5.1 calls a static method in an external class to retrieve a message.

Listing 5.1 Source Code for ReloadTest.jsp
1:  <%@ page language="java" import="examples.*" %>
2:  <html>
3:  <body>
4:
5:  The message is: <%= ReloadedClass.getMessage() %>
6:  </body>
7:  </html>

Listing 5.2 shows the ReloadedClass class that returns the message.

Listing 5.2 Source Code for ReloadedClass.java
1:  package examples;
2:
3:  public class ReloadedClass
4:  {
5:     public static String getMessage()
6:      {
7:          return "This is the original message";
8:      }
9:  }

Now when you run ReloadTest.jsp, you get the kind of result shown in Figure 5.2.

Figure 5.2. The JSP engine automatically loads the ReloadedClass Java class.

graphics/05fig02.gif

Now suppose you change the message to return a different message, like this:


return "This is the new message";

When you recompile the Java class and reload the JSP, you will not notice any change because the JSP engine is still holding on to the original.

Now suppose you change the ReloadTest class to have a slightly different message, like:


The new message is: <%= ReloadedClass.getMessage() %>

When you reload the JSP, the JSP engine recompiles the page, but the screen still looks the same. When the JSP engine loads the recompiled servlet into memory, it sees that it already has ReloadedClass in memory, so it doesn't bother to reload it again.

Now, if you restart the JSP engine and display the page again, you can see that the changed ReloadedClass class finally gets reloaded as shown in Figure 5.3.

Figure 5.3. When the JSP engine is restarted, it picks up the changed ReloadedClass class.

graphics/05fig03.gif

Using WAR Files

graphics/didyouknow_icon.gif

If you package your application in a WAR file and deploy it to the server using a deployment tool, the server usually refreshes all the classes in the WAR file automatically. It's a great idea to use Apache's ANT tool to automate the compiling, repackaging, and deploying of a Web application.


In Case of Trouble

graphics/bytheway_icon.gif

If you are having trouble reloading classes, see the "Q&A" section at the end of this hour.


    [ Team LiB ] Previous Section Next Section