Debugging
The testing/debugging phase is one of the hardest aspects of developing servlets. Servlets tend to involve a large amount of client/server interaction, making errors likely—but hard to reproduce. It can also be hard to track down the cause of nonobvious errors because servlets don’t work well with standard debuggers, since they run inside a heavily multithreaded and generally complex web server. Here are a few hints and suggestions that may aid you in your debugging.
Check the Logs
When you first think there might be a problem, check the logs. Most
servers output an
error log where you can find a list of
all the errors observed by the server and an
event log where you can find a list of
interesting servlet events. The event log may also hold the messages
logged by servlets through the log()
method, but
not always.
Note that many servers buffer their output to these logs to improve performance. When hunting down a problem, you may want to stop this buffering (usually by reducing the server’s buffer size to 0 bytes), so you can see problems as they occur. Be sure to reset the buffer size to a reasonable value afterward.
Output Extra Information
If you don’t see an indication of the problem in the
server’s logs, try having your servlet log extra information
with the log()
method. As you’ve seen in
examples elsewhere in this book, we habitually log stack traces and
other error situations. During debugging, you can add a few temporary
log()
commands as a poor man’s debugger, ...
Get Java Servlet Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.