Servlet Debugging
How do we debug a servlet application? Since the servlet engine is running our code what is available to us?
Locating Tomcat Logs
-
If you haven't already added the tomcat directory to your workspace, please do so now by following these instructions.
-
Expand the
logs
directory.
Reading the Tomcat Logs
When reading a log file, it's is helpful to start a the bottom of the file to find the most recent activity. In this class, you will use the catalina.out and localhost.log for debugging; however, there are other log files kept by tomcat.
catalina.out
This is the main Tomcat server log file. It contains information about Tomcat's startup, shutdown, and general server messages. Any System.out.println statement will appear here. Because System.out.println statements in the file can affect performance, all System.out.println statements should be removed prior to deploying an application to production.
localhost.log
This log file contains information specific to the Tomcat's "localhost" web application (aka your codespaces environment). It logs access requests and errors related to this application. Any log() statements will appear here.
Other Log files
localhost_access_log.txt
This log file records every HTTP request made to the Tomcat server, including details such as IP addresses, request URLs, response status codes, and user agents. It's useful for monitoring web traffic.
catalina.YYYY-MM-DD.log
These are daily log files generated by Tomcat, where "YYYY-MM-DD" represents the date. They contain detailed information about Tomcat's operation, including request processing, session management, and any exceptions or errors.
manager.YYYY-MM-DD.log
Similar to catalina logs, these files are generated daily and contain information specific to the Tomcat Manager application. They log activities related to deploying and managing web applications through the Tomcat Manager.
host-manager.YYYY-MM-DD.log
Like manager logs, these files are generated daily and contain information about the Tomcat Host Manager application, which is used for managing virtual hosts in Tomcat.
Other Debugging Tips
-
Plan Ahead! Make sure your data is "clean". Check for null data or empty values that could cause an issue in your application.
-
Check Browser Debuggers
- Look at the HTML source code or debugger tools in Firefox or Chrome to validate the HTML.
- Look at the request and response header data. We looked a few techniques to accomplish this.
-
Check the URL Is the URL correct and what you expect it to be in the browser?
-
Restart Tomcat Stop and start (called "bouncing") the server with
ant tcbounce
. -
Delete the .war File. You can delete the .war file located on the server. This will need to be via the terminal, so check the Unix Cheat Sheet for the correct commands.
-
Test Outside of Tomcat Write separate classes that are not servlets so that you can test them outside of Tomcat.
-
Log4J (not used in this class) In Enterprise Java, you'll use Apache Log4J. This is a Java-based logging library that allows developers to record and manage log messages in their applications.