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

back next

Item Management (continued)


View Page


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.


Implement ViewNewsItemServlet

Create ViewNewsItemServlet with the contents of the following listing.

package publisher.web;


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;


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
       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="" %>

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

<%@ include file="" %>


         <a href="${newsItem.url}">test link</a>


<%@ include file="" %>


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.