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

back next

Item Management (continued)

Video

View Page

Overview

So far, we have created the functionality for a single page: the page that lets the user see a list of news items in the system. In this section, we will create functionality needed to display the details of a particular news item.

Look at the page flow diagram. We have finished the page to list items and we are starting to create the page to view an item. This page provides menu choices to edit or delete the news item being viewed.

Configure a New Servlet in the Deployment Descriptor

On the list news items page, we render each news item title as a hyperlink that invokes a servlet through the URL view-news-item?id=n, where n is the id of the news item. Therefore, we need to modify the web.xml to route requests that match this pattern to a serlvet that can return the desired page. To do this, add to the deployment descriptor the servlet and servlet-mapping elements shown in the following listing.

 <servlet>
   <servlet-name>view-news-item</servlet-name>
   <servlet-class>publisher.web.ViewNewsItemServlet</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>view-news-item</servlet-name>
   <url-pattern>/view-news-item</url-pattern>
 </servlet-mapping>

Implement ViewNewsItemServlet

Create ViewNewsItemServlet with 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 ViewNewsItemServlet 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/view-news-item.jsp");
   }
   
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
   throws ServletException, IOException
   {
       logger.debug("doGet()");
       String idString = req.getParameter("id");
       Long id = new Long(idString);
       NewsItem newsItem = new NewsItemDAO().find(id);
       req.setAttribute("newsItem", newsItem);
       jsp.forward(req, resp);
   }
}

Create JSP

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

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

   <a href="edit-news-item?id=${newsItem.id}">Edit</a>
   <a href="delete-news-item?id=${newsItem.id}">Delete</a>

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

<table>
   <tr>
      <td>Title:</td>
      <td>${newsItem.title}</td>

   </tr>
   <tr>
      <td>Url:</td>
      <td>
         ${newsItem.url}
         <a href="${newsItem.url}">test link</a>

      </td>
   </tr>
</table>   

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

Test

Stop and start the publisher application with the Tomcat manager application, so that Tomcat reloads the web.xml file and the new servlet. Verify that the new functionality works correctly by going to http://localhost:8080/publisher/home and following the link to the list items page and then clicking on a title to go to the view item page for that news item. The following figure shows approximately how the view news item page should render.

View news item page

back next

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