Process a request by forwarding to the appropriate dispatcher
HttpSession session = request.getSession();
ServletContext context = getServletContext();
// get the last element of the request in lower case
String reqPath = request.getPathInfo();
reqPath = Character.toUpperCase(reqPath.charAt(1)) +
reqPath.substring(2).toLowerCase();
// find the dispatcher in the session
Dispatcher dispatcher =
(Dispatcher) session.getAttribute(reqPath + DISPATCHER_ATTR);
// if no dispatcher was found, create one
if (dispatcher == null) {
String className = reqPath + "Dispatcher";
try {
Class c = Class.forName(DISPATCHER_PREFIX + className);
dispatcher = (Dispatcher)c.newInstance();
} catch (Exception ex) {
throw new ServletException("Can't find class " + className, ex);
}
dispatcher.setContext(context);
session.setAttribute(reqPath + DISPATCHER_ATTR, dispatcher);
}
// make sure we don't cache dynamic data
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
// use the dispatcher to find the next page
String nextPage = dispatcher.getNextPage(request);
// forward control to the view
RequestDispatcher forwarder = request.getRequestDispatcher("/" + nextPage);
forwarder.forward(request, response);