You can use the Debug applet or the application's JDB command to debug the Servlet.
To debug a servlet, we can debug sun.servlet.http.HttpServer and then treat it as a httpserver execution servlet in response to the browser's HTTP request. This is very similar to debugging applet applets. Unlike debugging applets, the actual program being debugged is sun.applet.AppletViewer.
Most debuggers automatically hide the details of how to debug applets. Similarly, for servlets, you must help the debugger do the following:
- Set your debugger's classpath classpath so that it can find sun.servlet.http.http-server and related classes.
- Set your debugger's classpath classpath so that it can find your servlet and supported classes, typically in Server_root/servlets and server_root/classes.
You typically do not want server_root/servlets in your classpath because it disables the re-loading of the servlet. However, this inclusion rule is very useful for debugging. It allows your debugger to set breakpoints in the servlet before the custom servlet loader in httpserver loads the servlet.
If you have set the correct classpath classpath, you can start debugging Sun.servlet.http.HttpServer. You can set breakpoints in the Servlet code that you want to debug, and then use a given servlet (Http://localhost:8080/servlet/ServletToDebug) to make a request to httpserver through a Web browser. You will see that the program execution stops at the breakpoint.
Using the JDB debugger