Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Hubert Klein Ikkink (aka Mr.Haki) - Communications Officer
 Mr.Haki's JBuilder Jar #27
See Also: JBuilder Papers

Debugging servlets in the JBuilder IDE
Servlets are great, but developing servlets can be quit difficult. Compiling, copying to the web server, activating servlet through a web page, look at what goes wrong, change source, compiling... There must be easier way to do this, and there is!

To be able to debug a servlet in the JBuilder IDE, we must create a web server written in Java, and start this from the JBuilder IDE. Then we can activate the servlet through a web page and we are able to debug our servlet using the JBuilder debugger. The web server is already included with the JSDK (Java Servlet Development Kit): sun.servlet.http.HttpServer. This class is a simple Java web server capable of executing servlets.

Next we must write a Java application that will create an instance of HttpServer and start the web server. This application functions as a wrapper for the web server. The following code is all we need to write:

  import sun.servlet.http.*;

  public class DebugServer {
    public static void main(String[] args) {
      HttpServer webserver = new HttpServer();
      webserver.run();
    }
  }
When this application is executed a small web server will be started, which listens on port number 8080 to HTTP requests.

Add the source file with the previous code to a project. Then create a servlet and add this to the project. Let's create a simple servlet using the Servlet Wizard:

  1. Click on File | New.
  2. Select the Servlet icon.
  3. Leave all settings the same in the Wizard dialog and click on Finish.
  4. You now have a servlet source file and a HTML page to activate the servlet.
  5. Go the source of the HTML page.
  6. Change the text yourServerUrl/ in the FORM tag to localhost:8080.
  7. Set a breakpoint in the servlet source file:

    Setting breakpoint

  8. Select the DebugServer file and select Run | Debug.
  9. Load the HTML page in your web browser
  10. Click on the Submit button in the HTML page.
  11. The servlet will be executed and when we reach the breakpoint the execution will stop:

    Reached breakpoint

  12. Step through the code or look at variable values:

    Stepping through servlet

We could of course write the web server wrapper application everytime we develop servlets. But we can better save this application and add the source file to every project in which we want to debug servlets. Once we have added the source file of DebugServer we make it the default runnable file of our project.

  1. Go to File | Project properties...
  2. Select the Run / Debug page.
  3. Select DebugServer from the list of default runnable files.
  4. Click on OK.

Now when we execute the Debug command the web server will be started and awaiting requests from the web browser.


This webpage © 1997-2009 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.