Servlets

Setting Up the Servlet's J2EE Application

Create a J2EE application called Deitel1.

Compiling a Servlet

When compiling a servlet, you must include the j2ee.jar file in the classpath. One way to do this is to create a batch file, for example compile.bat:

set J2EE_HOME=C:\j2sdkee1.2.1
set CPATH=.;%J2EE_HOME%\lib\j2ee.jar
javac  -classpath %CPATH% %1

Then you can enter

compile HTTPGetServlet.java

Creating a Servlet's .war File

To package servlet class and html files in a .war file, you run the New Web Component Wizard of the Application Deployment Tool. To start the wizard, from the File menu choose New Web Component. The wizard displays the following dialog boxes. (You may skip any dialog boxes not listed here.)

WAR File General Properties Dialog Box:

a. In the combo box labelled "Web Component Will Go In," select Deitel1.
b. In the WAR Display Name field, enter GetServlet.
c. Click Add.
d. In the Add Content Files dialog box, you will be given two opportunities to enter files. The first is the AddContentFiles dialog box, for html and image files, and the second is the AddClassFiles, for class files. In this example, there are no content files so skip that dialog by clicking Next.
e. In the Add Class Files dialog box browse to your work directory and choose HTTPGetServlet.classes/adder from the text area and click Add.
f. Click Finish.
g. Click Next.
Choose Component Type Dialog Box:
a. Select Servlet.
b. Click Next.

Component General Properties Dialog Box:

a. In the Servlet Class combo box, select AdderServlet.
b. In the Web Component Display Name field, enter GetServlet.
c. Click Next.

Component Aliases Dialog Box:

a. Click Add.
b. In the Aliases list, enter GetDemo.
c. Click Finish.

Specifying the Web Context Root

1. In the tree view select AdderApp.

2. In the Web Context tabbed pane, enter servlets in the ContextRoot column.

Deploying the Servlet's J2EE Application

1. From the Tools menu, choose Deploy Application.

2. In the first dialog box, do not select the checkbox labelled "Return Client Jar."

3. In the third dialog box, verify the context root.

Running the Servlet

To run the HTTPGetServlet program from your browser, specify the URL as follows, but replace <host> with the name of the machine that is running the J2EE server:

http://<host>:8000/servlets/GetDemo


Maintained by John Loomis, last updated 5 Jan 2001