Lab 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CSC 584 ENTERPRISE PROGRAMMING

LAB 6: SERVLET -3
Post Lab Activities
Task 1 Developing the Add Form View Servlet

1. Create a java servlet using following characteristic:


▪ Class Name : AddDVDFormServlet
▪ Project : DVDLibrary
▪ Location : Source package
▪ Package : com.dvd.view
▪ Servlet Name : AddDVDFormServlet
▪ URL Pattern: /add_dvd.view
▪ Initialization parameters : None

2. Add a field to the AddDVDServlet class with the following characteristic:


a. Name: genres
b. Type: String[ ]
c. Access: private
d. Initial value : null

3. Add a method to the AddDVDServlet class with the following characteristic:


a. Name: init
b. Return type: void
c. Access: public
d. Parameters: none
e. Exceptions: ServletException

4. Within the init method, retrieve the genre-list initialization parameter. You should split method on this value to
populate the genres instance variable. The delimiter of the string is a comma.

5. Within the processRequest method, use print statements to generate the HTML form. You should use the
add_dvd.html static page as a guide to implement the processRequest method.

6. Replace the section that displays the Genre drop-down list with values in the genres variable.

MMR@FSKM2018
CSC 584 ENTERPRISE PROGRAMMING

Task 2: Displaying error information in the application

1. Modify the AddDVDFormServlet Servlet


a. Open the AddDVDFormServlet class in the com.dvd.view source package.
b. Within the processRequest method of the AddDVDFormServlet, retrieve the errorMsgs attribute from
the request-scope. The errorMsgs attribute is a List object, so you need to import the List interface.
c. If the errorMsgs attribute is not null, display the stings contained within the list.

You can use the processRequest method of the ErrorPageServlet class as a guide to implement the
display of the error messages.

2. Modify the AddDVDServlet Servlet


a. Open the AddDVDServlet class in the com.dvd.controller source package.
b. Modify the request dispatcher path from the /error.view tp /add_dvd.view in the sections of code that
perform the dispatch if errors are encountered.

3. Remove the ErrorPageServlet Servlet


a. Delete the ErrorPageServlet class from the com.dvd.view source package.
b. Open the web.xml deployment descriptor
c. Verify that the servlet definition and servlet mapping for ErrorPageServlet have been removed.

4. Deploy and test the web application


a. Build the DVDLibrary web application. Correct any errors you encounter.
b. Deploy the DVDLibrary web application.
c. Refresh the DVDLibrary web application home page
d. Click the link to add a DVD.
e. Test the application to see what happens when the form is not filled in correctly

MMR@FSKM2018
CSC 584 ENTERPRISE PROGRAMMING

Task 3: Repopulating Form Data

1. Modify the addDVDFormServlet class


a. Open the AddDVDFormServlet class in the com.dvd.view source package.
b. For each form field, repopulate the field using the request parameter data.
Hint:

To redisplay a text form element, use the code similar to:

String title = request.getParameter("title");


// Display the title field
out.print(" Title: <input type='text' name='title' ");
if(title == null) {
title = "";
}
out.println("value = '" + title + "' /> <br/><br/>");

To redisplay a selected option within a select form element , use the code similar to:

String genre = request.getParameter("genre");


// Repopulate the Genre drop-down menu
out.println(" Genre: <select name='genre'>");
for ( int i = 0; i < genres.length; i++ ) {
out.print(" <option value='" + genres[i] + "'");
if(genre != null && genre.equals(genres[i])) {
out.print(" SELECTED");
}
out.println("> " + genres[i] + "</option>");
}
out.println(" </select>");
out.print(" or new genre: <input type='text' name='newGenre' ");
if(newGenre == null) {
newGenre = "";
}
out.println("value = '" + newGenre + "'/> <br/><br/>");

2. Deploy and test the web application


a. Build the DVDLibrary web application. Correct any errors you encounter.
b. Deploy the DVDLibrary web application.
c. Refresh the DVDLibrary web application home page
d. Click the link to add a DVD.
e. Test the application to see what happens when the form is not filled in correctly

MMR@FSKM2018

You might also like