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

back next

Wiki Application (continued)


Unpublish Page


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 following listing shows the contents of unpublish-page.jsp.


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

<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="${}" />

The Unpublish Page Servlet

The following listing shows the contents of

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 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 {
		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);
		req.setAttribute("wikipage", page);
		jsp.forward(req, resp);

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 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);

		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);

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

		// 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.



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.