Java Web Programming with Eclipse contents
Last modified March 06, 2011 11:14 pm

back next

Item Management (continued)

Exercises

(1) Add link to list items page

Modify the list-news-item page, so that the url of the news item is displayed along with the news item title.

(2) Bad item id

Enter the URL http://localhost:8080/publisher/view-news-item?id=9999 into your browser and observe what happens. In our code we do not handle the case when the news item servlet receives a request with an invalid news item id. Change the news item servlet so that when an invalid news item id is received, the user is redirected to the list-news-item page. The following statement shows how to redirect the browser to the list news items page.

 
resp.sendRedirect("list-news-items");

(3) Multi-user problems

In the previous exercise, you looked at how a user can deliberately input invalid data to make the application malfunction. However, it is possible that the web application malfunctions in the same way in another scenario in which the user is not trying to deliberately break the system. Explain the scenario in which the user innocently clicks on a news item link in the list-news-item page and is sent to the view-news-item page with an invalid id. (Hint: the scenario requires that there are at least two users logged in at the same time, or that one user is logged in with the same username in two different browser instances.)

Carry out the scenario you describe in order to verify that your answer is correct.

(4) Reducing code duplication through inheritance

This problem is preparation for the next problem, which illustrates how to reduce code duplication by moving common functionality into parent classes. For this exercise, create a parent class called ServletParent in the publisher application, and have your serlvet classes extend it. Move the jsp variable from your servlet classes into ParentServlet. (Change the modifier from private to protected so that the jsp variable can be accessed by subclasses.)

(5) Servlet initialization parameters

This exercise assumes you have completed the previous exercise. Eliminate the init method from all of your servlets and create a single init method in ParentServlet. This init method will set the jsp path from an initialization parameter passed into it through the web.xml file.

In the web.xml file, modify the servlet declarations in order to pass in an initialization parameter called jsp. The jsp initialization parameter should be accessed inside the init method of ParentServlet. To carry out this exercise, you will need to perform research to find out how to do two things:

Note: to solve this problem you need to call getInitParameter() on the config object passed into the servlet's init method; do not call getInitParameter() on the servlet context object.

(6) Graceful user error handling

Consider the following scenario. Suppose user A obtains an edit form for a particular NewsItem and then user B deletes this newsItem. When A submits changes to this news item, the EditNewsItemServlet will call the update method of NewsItemDAO, which issues an SQL update command to the database. This update command will fail because a record with the given id will not exist in the news_item table.

Modify the application so that it handles this situation gracefully in the following manner. In the publisher.data package, create a class called NotFoundException that extends the Exception class. Throw this exception from within the update method of the DAOs when a servlet tries to update an object that doesn't exist in the database. (You should simply create a class called NotFoundException that extends Exception; you do not need to implement any methods in this class.)

In the EditNewsItemServlet, if NotFoundException is thrown, then set a message attribute in the request object and forward to the home jsp. In the home jsp, look for the presence of the message attribute and display it if present. This will also serve as a means to inform the user of other possible abnormal conditions. The following lines of code can be added to home.jsp to accomplish this.

<% 
    String message = (String) request.getAttribute("message");
    if (message != null) {
             out.println("<p>" + message + "</p>");
    }
%>

Before implementing the solution to this problem, carry out the senario to set how the application malfunctions.

back next

Copyright 2007-2009 David Turner and Jinseok Chae. All rights reserved.