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).
Debugging Servlets - How to print debugging data to the console (Page 2 of 4 )
If you’re using Tomcat in a stand-alone development environment, you can print debugging messages to the console for the servlet engine as shown in figure 5-10. To do that, you can use the println method of the System.out and System.err objects. You can use these message to help track the methods that are executed or the changes to variables.
If you aren’t using Tomcat in a stand-alone environment, you might not be able to view the debugging messages in the console. In some cases, though, you may be able to make them appear in the console by starting the servlet engine from a command line. In other cases, the println statements may automatically be written to a text file that you can view. Otherwise, you’ll need to print this data to a log file as described in the next figure.
When you use println statements to check the value of a variable, you’ll often want to include the name of the class and the name of the variable. That way, your messages will be easier to understand. This also makes it easier to find and remove the println statements once the error is debugged.
How Tomcat displays printin statements
Figure 5-10How to print debugging data to the console
When you’re testing an application on your local system, you may be able to use the println method of the System.out or System.err objects to display debugging messages on the console for the servlet engine.
When you use debugging messages to display variable values, it’s a good practice to include the class name and variable name so the messages are easy to interpret.