Java Web Programming with Eclipse contents
Last modified February 15, 2011 05:11 am

back next

Wiki Application (continued)


View Page


In this section we create and configure the servlet and jsp needed to let users view wiki pages.


Within the web/WEB-INF folder, create a new folder called jsp. Within the jsp folder create a file called view-page.jsp with the contents of the following listing.


<jsp:useBean id="wikipage" scope="request" type="" />
   <link rel="stylesheet" type="text/css" href="../styles.css" />

<table border="0" cellspacing="0" cellpadding="0">
      <td><img src="../images/logo.gif"></td>
      <td id="upper-bar"> 
         <div id="upper-menu">
            <a href="../edit/${}">edit</a>
               if (wikipage.isPublished()) {
            <a href="../unpublish/${}">unpublish</a>
               } else {
            <a href="../publish/${}">publish</a>
      <td id="lower-bar">
         <div id="layout">


Observe that the page is parameterized by a single variable, wikipage. Before forwarding to this JSP, the servlet constructs the wikipage instance and places it in request scope by associating the name wikipage with an instance of the page class. We avoid using the name page for this purpose because this is a reserved identifier used by JSP to reference something called the JSP page object.

The view page uses a table to organize the layout of the page. After the useBean element at the top of the JSP, the next instance of dynamic content is the construction of an edit link. In this edit link, we add the name of the wiki page that we are viewing. When the user clicks the link to edit the page, the browser sends a request with the string edit/ followed by the wiki page name.

After constructing the edit link, the JSP file contains a conditional expression that test whether or not the page is published in the publisher application. If the page is published, then the JSP generates a link that goes to the unpublish servlet. If the page is not published, the JSP generates a link that goes to the publish servlet. These two servlets will be constructed in a later section.

The last piece of dynamic content to appear in the view jsp file is the page content.

Notice that the image tag refers to a logo file in the images folder. This logo will be displayed as the page banner. You should create this folder and place a logo file in there.

Also notice that the JSP contains in the HTML head section a reference to a style sheet. You should create this file in the web folder with the following content.


h1 {
   color: #000088;

#layout {
	margin: 1em;

#upper-menu {
	margin: 0.5em;
	text-align: right;

#upper-bar {
	background-color: #E6E6FA;	

#lower-bar {
	background-color: #F5F5DC;	

The View Page Servlet

Add the view page servlet to the project with the following implementation.

package wiki.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 ViewPageServlet 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-page.jsp");

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String pathInfo = req.getPathInfo();
		String name = pathInfo.substring(1);
		logger.debug("Page requested: " + name);
		Page page = new PageDAO().find(name);
		if (page == null) {
			logger.debug("page doesn't exist; creating empty page");
			page = new Page();
		req.setAttribute("wikipage", page);
		jsp.forward(req, resp);


In a manner similar to the servlets in the publisher application, we override the init(ServletConfig) method in order to set the value of the request dispatcher that we will use to forward execution to the JSP.

The first thing we do after writing a log message is to determine the page name being requested b ythe user. We call getPathInfo on the request object to get the string within the url that follows /view. Because this string begins with a slash, we remove the slash with the substring method. This leaves the name of the wiki page. We print the page name in the log file, and then we use the name to retrieve the page object from the database.

If the page is not in the database, the page DAO will return null. In this case, we create a new page instance, set its name property to the page name being requested. We also set the page content to the empty string and the publsihed attribute to false.

After we have either retrieved an existing page or creted a new empty one, we put the page object in request scope by associating it with the name wikipage. The JSP will now be able to access the information it needs from this page object.

The Deployment Descriptor

Add the following deployment descriptor (web.xml) to the wiki application.

<?xml version="1.0"?>


Deploy the wiki application to the path /wiki in a manner similar to how you deployed the publisher application. The context element will look like the following. Make sure you replace ${WORKSPACE} with the path to your eclipse workspace. Additionally, make sure the database parameters are correct.

<Context path="/wiki" docBase="${WORKSPACE}\wiki\web">
    <Resource name="datasource"  
              url="jdbc:mysql://localhost:3306/wiki?autoReconnect=true" />

If you are not sure that you deployed the application correctly, then check to see that deployment succeeded by looking in ${TOMCAT}/conf/Catalina/localhost. You should see the file wiki.xml. Open this file and verify that it is identical to the wiki.xml given above that you placed in your wiki project folder.

If deployment succeeded, but the application is still not working correctly, then look in all the log files for messages. If there is an error related to connecting to the database, make sure that all the arguments in the context element in wiki.xml are correct.


Go to http://localhost:8080/wiki/view/ to view the page with no name as shown in the following screen shot. Click on the hello link and see that you go to the hello page. Click on the nowhere link and see that an empty page is generated.

View page

View page

back next

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