Debugging Servlets
Debug Servlets in Tomcat by running it as a debuggee that a debugger can attach to—and then do it remotely
by Kevin Jones
I teach Java programmers how to develop Web applications using Servlets/JSP and related technologies. The question I always get asked in my classes is, "How do I debug Servlets running inside Tomcat?"
The Java Platform Debugger Architecture (JPDA) provides support and infrastructure needed to debug Java applications and has been included in Java 2 Platform, Standard Edition since version 1.3. To debug a Java application (the debuggee) the Java Virtual Machine (JVM) has to be started with a JPDA server listening for connections. In the reference implementation the VM can listen in one of two ways: either with a socket or through shared memory (on Windows). Using a socket, of course, enables truly remote debugging.
To start a VM in debuggee mode, several run-time options have to be specified, and these options are passed to Sun's VM with the –X flags (note that –X flags are VM-specific flags that may not be supported in the next release) –Xdebug and –Xrunjdwp:[option, …]. The –Xdebug flag simply enables debugging. The –Xrunjdwp flag then allows various debugging options to be set, such as the name of the transport to be used and the address to listen on. This flag takes a comma-separated list of parameters. For example, you would call:
java myapp -Xdebug -
Xrunjdwp:server=y,
transport=dt_shmem,
address=jdbconn
This call says that the application should be started as a debuggee (server=y), that the transport to use is shared memory (transport=dt_shmem), and the name of the shared memory is jdbconn (address=jdbconn). There are two possible values for the transport—dt_shmem and dt_socket—so to listen as a socket server this would work:
java myapp -Xdebug -
Xrunjdwp:server=y,
transport=dt_socket,
address=9292
The Tomcat distribution has a script to start and stop the Tomcat Web server. For Windows this script is catalina.bat, and for Unix it is catalina.sh. These scripts accept a command line flag jpda that starts Tomcat as a debuggee using Tomcat's default options. For Windows the default server transport is dt_shmem and for Unix it is dt_socket. These transports come with the corresponding default addresses of jdbconn and 8000, but these values can be changed as you will see shortly. To start Tomcat as a debuggee, simply run:
catalina jpda run
or
catalina jpda start
Back to top
|