Hot swapping JSP and Java files in Tomcat

Posted on 29-01-2013 22:13 by graham
In this tutorial I will describe some useful tricks how you can hot swap certain components of your Web application deployed to Tomcat without restarting the server or redeploying the whole WAR file.

Tomcat directory structure


Let's say we are using standard Tomcat directory structure:
Tomcat directory structure
-- tomcat
-- work
-- webapps
-- myapp.war
-- ROOT
-- ...
-- bin
-- conf
-- lib
-- ...

If you put you app war file called myapp.war into the webapps folder and then start Tomcat, it will unpack the war file into a directory with the same name under webapps. In this case, a directory called myapp will be created. This folder will contain the exact same contents as the war file. For a typical web app, it will look like this:
-- webapps
-- myapp
-- WEB-INF
-- classes
-- ...
-- resources
-- META-INF

Note the classes directory under WEB-INF. This is where compiled classes of your components reside, and this is where you will replace them.

One more thing worth knowing is that JSP files that you have under myapp/WEB-INF/your-jsp-folder are compiled the first time they are invoked. So, during the first invocation of a JSP page, Tomcat compiles it to a Java file and stores it in its cache in <tomcat-home>/work/Catalina/your-app-name. The next time this JSP page is called, Tomcat does not look at the JSP file any more - it uses the Java compiled version.

Substituting JSP files


If you want to replace a JSP file for a newer one, you need to do two things:
1. Replace the JSP file in the unpacked myapp/WEB-INF folder.
2. Delete the buffered class from the work folder - the best thing is to remove the contents of <tomcat-home>/work/Catalina.

Substituting Java files


To substitute a Java class, you need to replace the correct .class (not .java) file in the myapp/WEB-INF/classes folder.

Problem with bean classes


If you are replacing a class that has been deployed in the application's context as a bean, replacing the class file as described above won't do the trick. This is because bean classes are instantiated once when the application context is created (which happens when the application is deployed) and their class files are not referenced any more. In this case you really need to redeploy the whole application for the changes to be applied.
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems