Home arrow Java arrow Debugging Servlets

Debugging Servlets

Last week, you learned about working with servlets. This week, you will learn how to debug servlets. This article, the last of three parts, is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189).

Author Info:
By: Murach Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
September 28, 2006
  1. · Debugging Servlets
  2. · How to print debugging data to the console
  3. · How to write debugging data to a log file
  4. · Perspective

print this article

Debugging Servlets
(Page 1 of 4 )

How to debug servlets

When you develop servlets, you will encounter errors. Thatís why this topic gives you some ideas on how to debug servlets when youíre using a text editor to develop your servlets. If youíre using an IDE, though, it may provide advanced debugging tools that let you step through code and monitor variables so you wonít the need to use the techniques that follow.

Common servlet problems

Figure 5-9 lists four common problems that can occur when youíre working with servlets. Then, it lists some possible solutions for each of these problems.

If your servlet wonít compile, the error message thatís displayed by the compiler should give you an idea of why the servlet wonít compile. If the compiler canít find a class thatís in one of the Java APIs, for example, you may need to install the API. If the compiler canít locate your custom classes, you may need to modify your classpath. And if the compiler has a problem locating a package, your package statement for the class might not correspond with the directory that contains the class.

If the servlet compiles but wonít run, it may be because the servlet engine isnít running. To solve this problem, of course, you can start the servlet engine. However, if the servlet engine is already running, you should double-check the URL to make sure that itís pointing to the correct host, path, and package for the servlet. A common mistake, for example, is to forget to include the package name when specifying a URL for a servlet.

If you make changes to a servlet and the changes arenít apparent when you test it, it may be because the servlet engine hasnít reloaded the modified class. Then, if youíre using Tomcat in a stand-alone development environment, you can either make sure that servlet reloading is turned on as explained in chapter 2. Or, you can shutdown Tomcat and restart it so the changed servlet will be reloaded the next time that itís requested.

If the HTML response page doesnít look right when itís rendered by the browser, the servlet is probably sending bad HTML to the browser. To fix this problem, you can use the Source command (for Internet Explorer) or the Page Source command (for Netscape) to view the HTML that has been returned to the browser. Then, you can identify the problem and modify the servlet to fix it.

Common servlet problems


Possible solutions

The servlet wonít compile

Make sure the compiler has access to the JAR files for all necessary APIs.


Make sure the classpath is pointing to the directory that contains your user-defined packages.


Make sure the class is in the correct directory with the correct package statement.

The servlet wonít run

Make sure the web server is running.


Make sure youíre using the correct URL.

The changes arenít showing up

Make sure servlet reloading is on, or shutdown and startup the server so it reloads the class that you modified.

The HTML page doesnít look right

Select the Source or Page Source command from your browserís View menu to view the HTML code. Then, you can read through the HTML code to identify the problem, and you can fix the problem in the servlet.


  • Appendix A shows how to install the Java APIs and how to set the classpath.

Figure 5-9   Common servlet problems

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials