Command Servlet

This is one of what I see as two alternate patterns in building Servlet Based systems that utilize the ModelViewController pattern. In this option a single servlet (a CommandServlet) is the entry point into the application. This means that the application has only a single URL. One of the parameters to the URL represents a "Command" that corresponds to a GoF "Command object" (e.g. the CommandPattern) that the CommandServlet will look up and execute.

Often the Command object will do some processing of information stored in the Http request and Session objects, and then place some result within some JavaBeans within the scope of the HTTP request and/or the HTTP session scope. The final act of the Command object is to return a result (often a boolean, but sometimes something more elaborate such as a Result Class) to the CommandServlet. The CommandServlet will look at this result and then find the suitable JSP page to display based on the result. A simple version of this would look like:

   Command Name -- > Successful page  |  Unsuccessful Page  |  Command Object

Where there is a hashtable or hashmap keyed to the command name that points to an object containing the successful page name, the unsuccessful page name, and the Command object instance.

The cool thing about this is that the page names can be customized in a text file without having to recompile code -- thus you can make simple changes to the application flow on the fly.

However, this only works if the flow of the application is mostly linear. Multiway branch conditions are hard to model. It is possible to model multiway branches by morphing this into an implementation of the StatePattern, but it's usually more complicated than it's worth.

The other alternative is to use OneServletPerAction?, which is a more flexible approach for handling "bushy" application branching.


EditText of this page (last edited July 22, 2003) or FindPage with title or text search