Getting started
We will debug the following code:
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. io .*;
Public class MyHelloWorld extends HttpServlet
{
Public void doGet (HttpServletRequest req, HttpServletResponse res)
Throws ServletException, IOException
{
Res. setContentType ("text/html ");
PrintWriter out = res. getWriter ();
Out. println ("<HTML> ");
Out. println ("<BODY> ");
Out. println ("<HEAD> ");
Out. println ("<TITLE> Hello World Sample Servlet </TITLE> ");
Out. println ("<meta http-equiv =" content-Type "content =" text/html;
Charset = gb2312 "> Out. println ("</BODY> ");
Out. println ("</HTML> ");
// Out. close ();
}
}
First, prepare for running the Servlet engine. You can use Tomcat or Servletrunner.
To run the Servlet engine, configure CLASSPATH. CLASSPATH needs to find sun. servlet. http. HttpServer and its related classes. In addition, CLASSPATH also needs to find the Servlet File tools. jar in the/lib directory.
In this exercise, no GUI is visible because JDB is a command line interface.
Start the debugger
Start the Servlet engine. You can use Tomcat or Servletrunner.
Configure JVM to use the special boot class path-Xbootclasspath. The bootstrap path must include the rt. jar file and the tools. jar file. You can set the environment variable in the command line.
Start the Servlet engine to debug the MyHelloWorld Servlet. Start Tomcat or Servletrunner.
Now you need to start the debugger for the HttpServlet class. Input:
Jdb sun. servlet. http. HttpServlet
You should see the following response:
Initializing jdb...
The HTTPServer class contains the main () method for listening to Servlet access requests.
In addition, there is nothing wrong with the code. We just choose it for instance research.
Set breakpoints
Next, set the breakpoint in the doGet () method of MyHelloWorld.
> Stop in MyHelloWorld. doGet
Breakpoint set in MyHelloWorld. doGet
> Run
Run sun. servlet. http. HttpServer
Running...
The code for one-step debugging is now run as a debug Servlet. Enter:
Http: // yourmachine: 8080/servlet/MyHelloWorld
The debugger stops the breakpoint at the beginning of the doGet () method. This is displayed in the DOS window where the debugger is started. You can use the list command to view the position in the Servlet where the debugger stops.
Other JDB commands
You can use other commands to debug the code in a JDB session. The following is a sample in JDK 2.0:
Step-by-line single-Step debugging. Print the expression value. Locals prints the local variables of the current method. Threads lists all Threads. Help provides Help for JDB commands to list all command options and definitions.