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

back next

Wiki Application (continued)

Video

Unpublish Page

Overview

In this section you will create and configure the servlet and jsp needed to let users unpublish wiki pages. However, we will not actually make a connection to the publisher application until the next chapter on web services.

Remember that the publisher application uses 2 different approaches to web service invocation. The publish operation is done through submission of an XML document using the HTTP get method. In contrast, the unpublish operation is done through submission of a parameter embeded in the url with the HTTP post method. For this reason, there is no handling of XML in the code presented in this section. In a real application, one might want to adhere to one form of web service implementation rather than mix them as done here. However, we use both approaches in order to illustrate their differences. The differences will become apparent in subsequent sections when we complete the implementatins of the method stubs we use in the servlets in the wiki application.

The JSP

The following listing shows the contents of unpublish-page.jsp.

unpublish-page.jsp

<jsp:useBean id="wikipage" scope="request" type="wiki.data.Page" />
<p>
Are you sure you want to unpublish this page?
</p>

<form method="post">
   <input type="submit" name="publish-button" value="Unpublish" />
   <input type="submit" name="cancel-button" value="Cancel" />
   <input type="hidden" name="name" value="${wikipage.name}" />
</form>

The Unpublish Page Servlet

The following listing shows the contents of UnpublishPageServlet.java.

package wiki.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 wiki.data.Page;
import wiki.data.PageDAO;

public class UnpublishPageServlet 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/unpublish-page.jsp");
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		logger.debug("doGet()");
		String pathInfo = req.getPathInfo();
		String name = pathInfo.substring(1);
		Page page = new PageDAO().find(name);
		// Don't allow users to publish empty pages.
		if (page == null) {
			resp.sendRedirect("../view/" + name);
			return;
		}
		req.setAttribute("wikipage", page);
		jsp.forward(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		logger.debug("doPost()");

		// extract form data
		String pageName = req.getParameter("name");

		// Check for cancel button.
		String cancelButton = req.getParameter("cancel-button");
		if (cancelButton != null) {
			resp.sendRedirect("../view/" + pageName);
			return;
		}

		PageDAO pageDAO = new PageDAO();
		Page page = pageDAO.find(pageName);

		// Don't do anything if page doesn't exist or is already unpublished.
		if (page == null || !page.isPublished()) {
			resp.sendRedirect("../view/" + pageName);
			return;
		}

		// Invoke remote web service to unpublish page.
		logger.debug("invoking web service");
		unpublish(page);

		// Update page.
		page.setPublished(false);
		pageDAO.update(page);
		resp.sendRedirect("../view/" + page.getName());
	}

        private void unpublish(Page page)
        {
                // Implement later.
        }
}

The doPost method invokes the publisher's web service inside the unpublish method, which we will implement at a later time.

Deployment Descriptor

The following XML fragment needs to be added to the deployment descriptor to configure the unpublish servlet.

   <servlet>
      <servlet-name>unpublish-page</servlet-name>
      <servlet-class>wiki.web.UnpublishPageServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>unpublish-page</servlet-name>
      <url-pattern>/unpublish/*</url-pattern>
   </servlet-mapping>

Test

Stop and start the wiki application with the manager application. Then test the new functionality by starting at http://localhost:8080/wiki/view/. Try all possible combinations to verify that the application functions correctly. The unpublish confirmation page is shown in the following screen shot.

Unpublish page

Unpublish page

back next

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