01: package csdl.stackmvc.control;
02:
03: import java.io.IOException;
04:
05: import javax.servlet.RequestDispatcher;
06: import javax.servlet.ServletConfig;
07: import javax.servlet.ServletException;
08: import javax.servlet.http.HttpServlet;
09: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11:
12: import csdl.stackmvc.control.command.Command;
13:
14: /**
15: * The central servlet that serves as the controller in the MVC pattern
16: * for JSP page dispatching.
17: *
18: * @author Philip M. Johnson
19: * @author Jitender Miglani (did minor changes)
20: */
21: public class Controller extends HttpServlet {
22:
23: private String exceptionAttribute = "javax.servlet.jsp.jspException";
24: private String commandParameter = "CommandName";
25:
26: /**
27: * The init method that must be overridden in subclasses so that super.init is called.
28: *
29: * @param conf The servlet configuration information.
30: * @exception ServletException If errors occur during initialization.
31: */
32: public void init(ServletConfig conf) throws ServletException {
33: super .init(conf);
34: }
35:
36: /**
37: * Receives all user requests and dispatches to appropriate command class
38: * for processing. The command class is determined by the value of the "CommandName"
39: * parameter in the request; if none is supplied, then "Clear" is assumed.
40: * The command class that is dispatched from this method returns the page that should be
41: * displayed in response to the user request. The command class that is dispatched from
42: * this method is responsible for updating the request object with new attributes required by
43: * the JSP page.
44: *
45: * @param request The servlet request object.
46: * @param response The servlet response object.
47: * @exception ServletException If problems occur with the request or rsponse.
48: * @exception IOException If problems occur during request forwarding.
49: */
50: public void doPost(HttpServletRequest request,
51: HttpServletResponse response) throws ServletException,
52: IOException {
53:
54: //Every request to this servlet must include the "CommandName" parameter.
55: String commandName = request.getParameter(commandParameter);
56: RequestDispatcher dispatcher;
57:
58: // Start out with a cleared ErrorMessage attribute.
59: request.setAttribute("errorMessage", "");
60:
61: //If no commandName param, assume just "index.jsp" URL and initialize.
62: if (commandName == null) {
63: commandName = "Clear";
64: }
65:
66: // Now try to dispatch to the command class responsible for handling the requested Command.
67: try {
68: String commandClassName = "csdl.stackmvc.control.command."
69: + commandName + "Command";
70: Command command = (Command) Class.forName(commandClassName)
71: .newInstance();
72: Page page = command.process(request);
73: request.setAttribute("PageTitle", page.getTitle());
74: dispatcher = getServletContext().getRequestDispatcher(
75: page.getFileName());
76: } catch (Exception e) {
77: request.setAttribute(exceptionAttribute, e);
78: dispatcher = getServletContext().getRequestDispatcher(
79: Page.ERROR.getFileName());
80: }
81:
82: // Now forward the updated request object on to the page that will be returned to the user.
83: dispatcher.forward(request, response);
84: }
85:
86: /**
87: * Dispatches to doPost for processing.
88: *
89: * @param request The servlet request.
90: * @param response The servlet response.
91: * @exception ServletException If problems during request/response processing.
92: * @exception IOException If problems during the forward to the JSP page.
93: */
94: public void doGet(HttpServletRequest request,
95: HttpServletResponse response) throws ServletException,
96: IOException {
97: doPost(request, response);
98: }
99: }
|