F0102
Laboratory Exercise 7: Configuring Servlet
Using ServletConfig
At the end of the exercise, the students should be able to:
Configure servlet using ServletConfig API.
Configure the deployment descriptor.
Deploy a servlet in the Web container.
Materials:
PC with Java SDK and JCreator installed.
PC with J2EE 5.5 or later installed
PC with Apache Tomcat installed and configured
Perform the following instructions:
Part I.
In this exercise, you will use the servlet from Lab 6.
1. Create the following directory structure in the top-level directory, webapps, in the installation directory
of tomcat. The folder names are case sensitive.
Laboratory Exercise 7: Configuring Servlet Using ServletConfig * Property of STI
Page 1 of 4
F0102
2. Modify the BookstoreSurveyServlet so that aside from displaying the results of the survey on-the-fly, it
will also output the servlet initialization parameters. The parameters to be displayed are given in the
table below as name-value pairs:
Name Value
firstname <supply your firstname here>
lastname <supply your lastname here>
section <supply your section here>
date <the date you perform this exercise>
instructor <your instructor’s fullname>
3. Open a text editor. Type the following codes and save the file as ‘web.xml’. Save this file inside the
webapps/ADPROG1_LAB7/WEB-INF directory. Supply the actual values in the <param-value> tag.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Lab Exercise 7</display-name>
<description>
Configuring Servlets Using ServletConfig
</description>
<!-- Define servlets that are included in the
BookstoreSurveyServlet application -->
<servlet>
<servlet-name>BookstoreSurveyServlet</servlet-name>
<servlet-class>adprog1.web.BookstoreSurveyServlet</servlet-
class>
<init-param>
<param-name>firstname</param-name>
<param-value><!-- your first name here --></param-value>
</init-param>
<init-param>
<param-name>lastname</param-name>
Laboratory Exercise 7: Configuring Servlet Using ServletConfig * Property of STI
Page 2 of 4
F0102
<param-value><!-- your last name here --></param-value>
</init-param>
<init-param>
<param-name>section</param-name>
<param-value><!-- your section here --></param-value>
</init-param>
<init-param>
<param-name>date</param-name>
<param-value><!-- the date you perform this exercise --></param-
value>
</init-param>
<init-param>
<param-name>instructor</param-name>
<param-value><!-- your instructor's fullname here --></param-
value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>BookstoreSurveyServlet</servlet-name>
<url-pattern>/bookstore</url-pattern>
</servlet-mapping>
</web-app>
Part 2 - Developing the BookstoreSurvey Servlet
1. Modify the BookstoreSurvey servlet program that will retrieve form data, create an on-the-fly HTML
page, retrieve servlet config initialization parameters and display the contents of the form in a table and
the initialization parameters.
2. Modify the survey.html and change the value of the action attribute as specified in the url-pattern
declared in the web.xml above. Rename the survey.html to index.html and save the file in the
webapps/ADPROG1_LAB7 directory.
3. The sample response should look like the one shown in Figure 1.
Laboratory Exercise 7: Configuring Servlet Using ServletConfig * Property of STI
Page 3 of 4
F0102
Part 3 - Deploying and Activating the BookstoreSurvey Servlet
1. Compile the BookstoreSurveyServlet.java and store the .class file in webapps/ADPROG1_LAB7/WEB-
INF/classes/adprog1/web/ folder inside the installation directory of Apache Tomcat.
2. When the servlet class has been deployed and the Apache Tomcat has been started, you can activate
the servlet using a Web browser.
3. Launch the Web browser and enter the URL for the servlet in the Address field. The URL for this
servlet is:
http://localhost:8080/ADPROG1_LAB7/index.html
Note: The port number varies depending on the port number you specified in the sever.xml.
4. Enter data and click the Submit button. The servlet should respond and displays the form data in a
table (see sample output below).
Bookstore Survey Result
Figure 1: Sample Output
Laboratory Exercise 7: Configuring Servlet Using ServletConfig * Property of STI
Page 4 of 4