The Need for Separation, Part 2
As mentioned previously, a framework of sorts is the solution for adding power to the servlet controller + jsp view approach. To free the developer up from managing urls, and allowing them to have units of controller logic automatically tied to corresponding views (html, xml, txt, csv, etc.) is a decided improvement in application design. In the realm of rapid application development and agile programming practices, the ability to free the mind of these cumbersome tasks, with a standard organization structure improves the ability of the programmer to provide maintainable applications quickly and in a cost-effective manner.
The Front Controller
The cornerstone of an MVC framework is the front controller. This approach requires commitment to using one, and only one, entry point into the application for a given client type. For instance, a web browser client would be given access to every page of a web application through some standard url, like /MyServletController or /index.jsp.
The reasoning behind this approach is simple. First, it gives the software, and therefore the programmer, the ability to handle url management internally, instead of externally. Any mechanism for decomposing and rationalizing urls is available to the programmer. This eliminates the need to maintain url mappings. Also, in the context of agile, it allows for the rapid deployment of web components, with no more administrative tasks required than adding an <a> tag pointing to pages in the module component. The url would be automatically translated by whatever code sits behind the front controller, and control of the application can be routed to the appropriate module or component for processing--in a word, "automagically." In the servlet world, the request object gives the front controller the power to later forward control to a jsp page for view processing, so both the controller logic and the view logic can be handled in independent sections of the application.
Without further ado, let's see an example of a front controller servlet. This example uses (at this point) mystical java classes to do most of its' work.public class DispatcherServlet 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, IOException
{
ServletContext context = getServletConfig().getServletContext();
WebController controller = new WebController(context, request, response);
// dispatch the request to the "framework"
String view = controller.dispatch();
// forward control to the jsp view for processing
request.getRequestDispatcher(view).forward(request, response);
}
/**
* Handles the HTTP GET method.
*
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, 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, IOException
{
processRequest(request, response);
}
}
Next article: The Controller
0 comments:
Post a Comment