0% found this document useful (0 votes)
82 views2 pages

Lab 5 - Deploying A Simple Servlet

This document provides instructions for developing and deploying a simple servlet that generates a random quote of the day. The steps include: creating a QuoteServlet class, populating an array with quotes, generating HTML with the random quote, adding necessary libraries, compiling and deploying the servlet, and accessing it through a web browser at a specific URL. Hints are provided for generating a random number and initializing an array of quotes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views2 pages

Lab 5 - Deploying A Simple Servlet

This document provides instructions for developing and deploying a simple servlet that generates a random quote of the day. The steps include: creating a QuoteServlet class, populating an array with quotes, generating HTML with the random quote, adding necessary libraries, compiling and deploying the servlet, and accessing it through a web browser at a specific URL. Hints are provided for generating a random number and initializing an array of quotes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

F0102

Laboratory Exercise 5: Developing and Deploying


a Simple Servlet

At the end of the exercise, the students should be able to:


 Develop a simple servlet.
 Deploy a simple servlet.

Materials:
 PC with Java SDK and JCreator installed.
 PC with Java EE SDK 5.03 installed.
 PC with Apache Tomcat installed and configured

Perform the following instructions:

Write a servlet that generates a dynamic quote of the day. This servlet generates a dynamic HTTP
response by selecting a random quote of the day. You might want to use a constant array of strings to
hold the messages and then at runtime generate a random number based on the number of elements in
the array.

A.) Create the file QuoteServlet.java using JCreator.


B.) Declare that the Java technology class is in the adprog1.web package.
C.) Import all necessary packages.
D.) Create the QuoteServlet class.
E.) Create a static array of String objects. Populate this array with several messages of your choice.
F.) Generate the HTML response as described below. The HTML response might look like this:

<HTML>
<HEAD><TITLE>Quote of the Day Servlet</TITLE></HEAD>
<BODY BGCOLOR=’white’>
The quote of the day is:<BR>
<H3>the message</H3>
</BODY>
</HTML>

Laboratory Exercise 5: Developing and Deploying a Simple Servlet * Property of STI


Page 1 of 2
F0102

G.) In JCreator, add the javaee.jar in the project library. The javaee.jar file can be found at the
C:\j2ee_5.0\lib, assuming that the j2ee_5.0 is the installation directory of Java EE SDK.
H.) Compile the QuoteServlet.java and store the .class file in webapps/ROOT/WEB-
INF/classes/adprog1/web/ folder inside the installation directory of Apache Tomcat.
I.) When the servlet class has been deployed and the Apache Tomcat has been started, you can activate
the servlet using a Web browser. Verify that you can start the server. Double-click
install_dir/bin/startup.bat and try accessing http://localhost/.
J.) Launch the Web browser and enter the URL for the servlet in the Address field. The URL for this
servlet is:

http://localhost:8080/servlet/adprog1.web.QuoteServlet

HINTS:

Listed below are hints that might help you to complete this exercise.

o Use the Math.random method to generate a random double number between 0.0 and 1.0. Transform
this result to get an integer between 0 and N with code such as:

(int) (Math.random() * N)

o You might want to store the set of messages in a static array. An array can be initialized with the
following syntax:

private static final String [] MESSAGES = {″msg1″, ″msg2″, ″msg3″, ″msgN″}

Laboratory Exercise 5: Developing and Deploying a Simple Servlet * Property of STI


Page 2 of 2

You might also like