Example 7.29 Controller Servlet with Command and Controller Strategy
public class Controller extends HttpServlet {
/** Processes requests for both HTTP
* GET
and POST
methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String next;
try {
// Log pattern info
LogManager.recordStrategy(request,
"Service To Worker",
" ServletFront Strategy;" +
" JSPView Strategy; JavaBean helper Strategy");
LogManager.logMessage(request, getSignature(),
"Process incoming request. ");
// Use a helper object to gather parameter
// specific information.
RequestHelper helper = new
RequestHelper(request, response);
LogManager.logMessage(request, getSignature(),
"Getting command object helper");
// Get command object helper
Command command = helper.getCommand();
// delegate processing to the command object,
// passing request and response objects along
next = command.execute(helper);
/** If the above command returns a value, we
* will dispatch from the controller. In this
* example, though, the command will use a
* separate dispatcher component to choose a
* view and dispatch to that view. The command
* object delegates to this dispatcher
* component in its execute method, above, and
* control should not return to this point **/
}
catch (Exception e) {
LogManager.logMessage(
"EmployeeController(CommandStrategy)",
e.getMessage() );
/** ApplicationResources provides a simple API
* for retrieving constants and other
* preconfigured values**/
next = ApplicationResources.getInstance().
getErrorPage(e);
}
dispatch(request, response, next);
}
/** Handles the HTTP GET
method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
/** Handles the HTTP POST
method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet. */
public String getServletInfo() {
return getSignature();
}
/** dispatcher method */
protected void dispatch(HttpServletRequest request,
HttpServletResponse response,
String page) throws
javax.servlet.ServletException,
java.io.IOException {
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(page);
dispatcher.forward(request, response);
}
public void init(ServletConfig config) throws
ServletException {
super.init(config);
}
public void destroy() { }
private String getSignature() {
return "ServiceToWorker-Controller";
}
}