Controller.java :  » Code-Analyzer » JBlanket » csdl » stackmvc » control » Java Open Source

Java Open Source » Code Analyzer » JBlanket 
JBlanket » csdl » stackmvc » control » Controller.java
package csdl.stackmvc.control;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import csdl.stackmvc.control.command.Command;

/**
 * The central servlet that serves as the controller in the MVC pattern
 * for JSP page dispatching.
 *
 * @author Philip M. Johnson
 * @author Jitender Miglani (did minor changes)
 */
public class Controller extends HttpServlet {

  private String exceptionAttribute = "javax.servlet.jsp.jspException";
  private String commandParameter = "CommandName";


  /**
   * The init method that must be overridden in subclasses so that super.init is called.
   *
   * @param conf The servlet configuration information.
   * @exception ServletException If errors occur during initialization.
   */
  public void init(ServletConfig conf) throws ServletException {
    super.init(conf);
  }


  /**
   * Receives all user requests and dispatches to appropriate command class
   * for processing. The command class is determined by the value of the "CommandName"
   * parameter in the request; if none is supplied, then "Clear" is assumed.
   * The command class that is dispatched from this method returns the page that should be
   * displayed in response to the user request.  The command class that is dispatched from
   * this method is responsible for updating the request object with new attributes required by
   * the JSP page.
   *
   * @param request The servlet request object.
   * @param response The servlet response object.
   * @exception ServletException If problems occur with the request or rsponse.
   * @exception IOException If problems occur during request forwarding.
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {

    //Every request to this servlet must include the "CommandName" parameter.
    String commandName = request.getParameter(commandParameter);
    RequestDispatcher dispatcher;

    // Start out with a cleared ErrorMessage attribute.
    request.setAttribute("errorMessage", "");

    //If no commandName param, assume just "index.jsp" URL and initialize.
    if (commandName == null) {
      commandName = "Clear";
    }

    // Now try to dispatch to the command class responsible for handling the requested Command.
    try {
      String commandClassName = "csdl.stackmvc.control.command."
                              + commandName + "Command";
      Command command = (Command) Class.forName(commandClassName).newInstance();
      Page page = command.process(request);
      request.setAttribute("PageTitle", page.getTitle());
      dispatcher = getServletContext().getRequestDispatcher(page.getFileName());
    }
    catch (Exception e) {
      request.setAttribute(exceptionAttribute, e);
      dispatcher = getServletContext().getRequestDispatcher(Page.ERROR.getFileName());
    }

    // Now forward the updated request object on to the page that will be returned to the user.
    dispatcher.forward(request, response);
  }


  /**
   * Dispatches to doPost for processing.
   *
   * @param request The servlet request.
   * @param response The servlet response.
   * @exception ServletException If problems during request/response processing.
   * @exception IOException If problems during the forward to the JSP page.
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
    doPost(request, response);
  }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.