Java Web Programming with Eclipse contents
Last modified February 05, 2011 10:59 pm

back next

Item Management (continued)

Video

Delete Page

Overview

In this section, we will add functionality to enable the user to delete news items. The logic need for this task is similar to that used for the edit news item page.

Modify the news item DAO

Add the following implementation of the delete method to the news item DAO.

   public void delete(NewsItem newsItem)
   {
      PreparedStatement statement = null;
      Connection connection = null;
      try
      {
         connection = getConnection();
         String sql = "delete from news_item where id=?";
         statement = connection.prepareStatement(sql);
         Long id = newsItem.getId();
         statement.setLong(1, id.longValue());
         statement.executeUpdate();
      } catch (SQLException e)
      {
         throw new RuntimeException(e);
      } finally
      {
         close(statement, connection);
      }
   }

The delete method takes a news item instance as its sole argument. However, the delete method only needs the id attribute within the news item instance, which it uses within the where clause of an SQL delete command. Note that like the update method, it is possible that some other user deleted the object just prior to the execution of this delete method. However, in this case we silently ignore this unexpected scenario, because the user intends to delete the news item anyway. However, the application would give the user the impression that he or she deleted the news item rather that it being first deleted by someone else. If this false impression were to have negative consequences in some application scenario, then we would change the behavior of the delete method by throwing a NotFoundException in order to inform calling code of the abnormal completeion of the operation.

Modify the Deployment Descriptor

Add configuration to web.xml so that DeleteNewsItemServlet is invoked for requests matching /delete-news-item as the following listing.

<servlet>
   <servlet-name>delete-news-item</servlet-name>
   <servlet-class>publisher.web.DeleteNewsItemServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>delete-news-item</servlet-name>
   <url-pattern>/delete-news-item</url-pattern>
</servlet-mapping>

Create DeleteNewsItemServlet

Create DeleteNewsItemServlet by using the contents of the following listing.

package publisher.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import publisher.data.NewsItem;
import publisher.data.NewsItemDAO;

public class DeleteNewsItemServlet extends HttpServlet
{
   private Logger logger = Logger.getLogger(this.getClass());
   private RequestDispatcher jsp;

   public void init(ServletConfig config) throws ServletException {
      ServletContext context = config.getServletContext();
      jsp = context.getRequestDispatcher("/WEB-INF/jsp/delete-news-item.jsp");
   }
   
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
   throws ServletException, IOException
   {
      logger.debug("doGet()");
      jsp.forward(req, resp);
   }

   protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
   throws ServletException, IOException
   {
      String idString = req.getParameter("id");
      
      // Check if cancel button was pressed.
      String cancelButton = req.getParameter("cancel-button");
      if (cancelButton != null)
      {
         logger.debug("cancel button pressed");
         resp.sendRedirect("view-news-item?id=" + idString);
         return;
      }

      NewsItemDAO newsItemDAO = new NewsItemDAO();
      Long id = new Long(idString);
      NewsItem newsItem = newsItemDAO.find(id);
      new NewsItemDAO().delete(newsItem);
      resp.sendRedirect("list-news-items");
   }
}

The first thing we do inside doPost is to check for the cancel button. If pressed, we redirect the user to the view-news-item page so that he can verify that the news item has not been deleted.

If the cancel button was not pressed, we perform a find on the news item because we have designed the DAO delete method to take a NewsItem object. In case another user deleted the news item already, we check to make sure the news item is not null. If not null, we pass it to the delete method of NewsItemDAO.

Finally, we redirect the user to the list-news-items page so that he can verify that the news item was deleted.

Create the JSP

Create delete-news-item.jsp with the contents of the following listing.

<%@ include file="top.inc" %>
<%@ include file="middle.inc" %>

<p>
Are you sure you want to delete this news item?
</p>

<form method="post">
   <input type="submit" name="delete-button" value="Delete" />
   <input type="submit" name="cancel-button" value="Cancel" />
   <input type="hidden" name="id" value="${params['id']}" />

</form>

<%@ include file="bottom.inc" %>

Notice that we pass the id of the news item through a hidden input element.

Test

Stop and start the publisher application with the Tomcat manager application, so that Tomcat reloads the web.xml file and the new class files. Verify that the new functionality works correctly by going to http://localhost:8080/publisher/home and deleting a news item. You should also verify that the cancel operation works correctly. The following figure shows approximately how the create news item page should render.

Delete news item page

back next

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