Developer's Daily Java Education
  front page | java | perl | unix | DevDirectory
   
Front Page
Java
Education
Pure Java
Articles
   

Servlet Runner 101:
Testing your Java servlets
with the servletrunner
 
 
The servletrunner that's supplied with the JSDK is a useful utility for testing and debugging Java servlets before you deploy them to your web server. This article answers basic questions about the servletrunner, and provides a step-by-step description of how to test a simple "Hello, world" Java servlet using the servletrunner.
 

Introduction

Once you've developed a servlet with JBuilder, Visual Cafe, the Java JDK, or other development tools, you'll want to test the servlet. You can test it by plugging it right into your servlet-compatible web server, or you can take another approach -- testing it first with the servletrunner that comes with the Java Servlet Development Kit (JSDK).

In this article, we'll show you how to test simple Java servlets with the servletrunner.
 

Five questions about the servletrunner

1. What's a servletrunner?

The servletrunner is a small utility, intended for testing. 

It is multithreaded, so it can run more than one servlet. It can be used, therefore, to run multiple servlets simultaneously, or to test one servlet that calls other servlets in order to satisfy client requests. Unlike some web servers, it does not automatically reload servlets when they are updated. Because it is small, however, there is very little overhead associated with stopping and restarting it in order to use a new version of a servlet. 

(re-printed from the JSDK 2.0 documentation.)
 
2. Where's the servletrunner? 3. How do I start the servletrunner? 4. How do I get help on using the servletrunner? 5. The servletrunner is running; how do I stop it?  
Testing our JBHelloWorldServlet

In a related servlet article, we showed you how to create a simple "Hello, world" servlet using JBuilder 2. Let's quickly walk through the steps that are required to test that Java servlet in the servletrunner.

  1. Create the servlet. We performed this step in the JBuilder article.
  2. Copy the servlet to the C:\JAVA\JSDK2.0 directory. On my system, the compiled JBHelloWorldServlet.class file is located in my C:\Apps\Win95\Inprise\JBuilder2\Projects directory, so I just copy it using the Windows Explorer.
  3. Open a DOS window. Use the cd command to move to the C:\JAVA\JSDK2.0 directory.
  4. Start the servletrunner by running the srunner.bat program.
  5. Open Netscape (or another browser).
  6. Enter the URL for the servlet to be tested. In this case, the servlet is named JBHelloWorldServlet, so the URL is "http://localhost:8080/servlet/JBHelloWorldServlet".
  7. If everything worked as planned, the output of the servlet should appear in your browser.

Summary

The servletrunner that's supplied with the JSDK is a useful utility for testing and debugging Java servlets before you deploy them to your web server. This article answers basic questions about the servletrunner, and provides a step-by-step description of how to test a simple "Hello, world" servlet.  In a future article, we'll discuss the additional steps that may be required to test your more complicated Java servlets.


What's Related


Copyright 1998-2009 Alvin Alexander, devdaily.com
All Rights Reserved.