Main Content

Changing a Servlet - more that just editing and compiling

Archive - Originally posted on "The Horse's Mouth" - 2010-11-12 17:49:39 - Graham Ellis

If you're going to modify a servlet, you'll need to edit and recompile the classes that you're changing - and chances are that will include the main application class. Which means you need to have access to the source. Not a problem if you're a developer of the product, but could be an issue if it's someone else's code. Let's assume, though, for this article that you do have access to the source.

Do you just edit and compile, then?

Sorry - no - it's not that simple ...

1. Even though you may be compiling on a different machine to you'll be running on, you will need to have all the classes called available on the compile system. Which means all the application classes called, and also library classes which are used, need to be available. (Ironically - just to satisfy the compiler that your code is right!). In particular, many of the servlet classes may need to be sourced; from Tomcat, where the distribution includes servlet-api.jar , is the easiest source. And you then need that jar on your CLASSPATH.

2. You need to copy the compiled output into the WEB-INF/classes directory of your web application so that it's accessible to Apache Tomcat (or whatever other Servlet container you're using)

3. If you're renaming you application of its URL (perhaps you're putting up a test version alongside the production one), you'll need to add to / change your WEB-INF/web.xml file to provide an updated URL to class mapping

4. You're likely to need to stop and start Tomcat - or at least the web application - to clear cahces and reload, unless the URL and class names are brand new.

Details of these operations / stages are includes as comments on the top of a new servlet I set up on today's Java Course - the full code, and the instructions - are [here].