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

back next

Item Management (continued)

Video

Home Page

In this section, we add a home page for the publisher application. We implement the home page by defining a servlet that simply forwards requests to a jsp file.

There are several different sequences of activities we can take to implement the new functionality. In this section, we will use the following sequence.

  1. Configure a new servlet in the deployment descriptor.
  2. Implement the new servlet.
  3. Implement the jsp.
  4. Test.

Configure a new servlet in the deployment descriptor

Configure the home servlet by adding the following listing to the deployment descriptor.

 <servlet>
     <servlet-name>home</servlet-name>
     <servlet-class>publisher.web.HomeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>home</servlet-name>
     <url-pattern>/home</url-pattern>
  </servlet-mapping>

Create HomeServlet

Create a servlet called HomeServlet in the publisher.web package from 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;

public class HomeServlet 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/home.jsp");
   }

   protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
   throws ServletException, IOException {
      logger.debug("home page requested");
      jsp.forward(req, resp);
   }
}

Create JSP Files

Recall from the overview to this chapter that the JSP files will have the following structure.

 <%@ include file="top.inc" %>
   <!-- page menu -->
 <%@ include file="middle.inc" %>
   <!-- page content -->
 <%@ include file="bottom.inc" %>

Now, we are ready to create the jsp file that the home servlet will forward to in order to generate the HTML. Create a file home.jsp inside the jsp folder with the contents of the following listing.

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

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

<h1>Welcome</h1>

<p>
This is the publisher application.
It allows you to publish and manage a news feed.
</p>

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

Because the home page has no page specific operations for the user, the section between the top and bottom includes is empty.

Test

Have Tomcat reprocess the web.xml file by stoping and starting the publisher application through the manager application and verify that the application is running correctly by going to the home page of the publisher application. The following screen shot shows approximately how the home page is rendered.

Welcome page

back next

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