KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > stackmvc > control > Controller


1 package csdl.stackmvc.control;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.RequestDispatcher JavaDoc;
6 import javax.servlet.ServletConfig JavaDoc;
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServlet JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc {
22
23   private String JavaDoc exceptionAttribute = "javax.servlet.jsp.jspException";
24   private String JavaDoc commandParameter = "CommandName";
25
26
27   /**
28    * The init method that must be overridden in subclasses so that super.init is called.
29    *
30    * @param conf The servlet configuration information.
31    * @exception ServletException If errors occur during initialization.
32    */

33   public void init(ServletConfig JavaDoc conf) throws ServletException JavaDoc {
34     super.init(conf);
35   }
36
37
38   /**
39    * Receives all user requests and dispatches to appropriate command class
40    * for processing. The command class is determined by the value of the "CommandName"
41    * parameter in the request; if none is supplied, then "Clear" is assumed.
42    * The command class that is dispatched from this method returns the page that should be
43    * displayed in response to the user request. The command class that is dispatched from
44    * this method is responsible for updating the request object with new attributes required by
45    * the JSP page.
46    *
47    * @param request The servlet request object.
48    * @param response The servlet response object.
49    * @exception ServletException If problems occur with the request or rsponse.
50    * @exception IOException If problems occur during request forwarding.
51    */

52   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
53        throws ServletException JavaDoc, IOException JavaDoc {
54
55     //Every request to this servlet must include the "CommandName" parameter.
56
String JavaDoc commandName = request.getParameter(commandParameter);
57     RequestDispatcher JavaDoc dispatcher;
58
59     // Start out with a cleared ErrorMessage attribute.
60
request.setAttribute("errorMessage", "");
61
62     //If no commandName param, assume just "index.jsp" URL and initialize.
63
if (commandName == null) {
64       commandName = "Clear";
65     }
66
67     // Now try to dispatch to the command class responsible for handling the requested Command.
68
try {
69       String JavaDoc commandClassName = "csdl.stackmvc.control.command."
70                               + commandName + "Command";
71       Command command = (Command) Class.forName(commandClassName).newInstance();
72       Page page = command.process(request);
73       request.setAttribute("PageTitle", page.getTitle());
74       dispatcher = getServletContext().getRequestDispatcher(page.getFileName());
75     }
76     catch (Exception JavaDoc e) {
77       request.setAttribute(exceptionAttribute, e);
78       dispatcher = getServletContext().getRequestDispatcher(Page.ERROR.getFileName());
79     }
80
81     // Now forward the updated request object on to the page that will be returned to the user.
82
dispatcher.forward(request, response);
83   }
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 JavaDoc request, HttpServletResponse JavaDoc response)
95        throws ServletException JavaDoc, IOException JavaDoc {
96     doPost(request, response);
97   }
98 }
99
100
Popular Tags