Programming Tutorials

MVC with configurable controller

By: Charles in Struts Tutorials on 2007-09-13  

When application gets large you cannot stick to bare bone MVC. You have to extend it somehow to deal with these complexities. One mechanism of extending MVC that has found widespread adoption is based on a configurable controller Servlet. The MVC with configurable controller servlet is shown in Figure below.

When the HTTP request arrives from the client, the Controller Servlet looks up in a properties file to decide on the right Handler class for the HTTP request. This Handler class is referred to as the Request Handler. The Request Handler contains the presentation logic for that HTTP request including business logic invocation. In other words, the Request Handler does everything that is needed to handle the HTTP request. The only difference so far from the bare bone MVC is that the controller servlet looks up in a properties file to instantiate the Handler instead of calling it directly.

MVC with configurable controller Servlet. 

// Configurable Controller Servlet Implementation 
public class MyControllerServlet extends HttpServlet {
    private Properties props;

    public init(ServletConfig config) throws ServletException {
        
    try {
    props = new Properties();
    props.load(new FileInputStream("C:/file.properties"));
        
    } catch (IOException ioe) {
    throw new ServletException(ioe);
    }
    }

    public void doGet(HttpServletRequest httpRequest,
    HttpServletResponse httpResponse)
    throws ServletException, IOException {
       
    String urlPath = httpRequest.getPathInfo();
    String reqhandlerClassName = (String) props.get(urlPath);
        
    HandlerInterface handlerInterface = (HandlerInterface)
    Class.forName(reqhandlerClassName).newInstance();
        
    String nextView = handlerInterface.execute(httpRequest);
    ..
    ..
    
    RequestDispatcher rd = getServletContext().
    getRequestDispatcher(nextView);
    rd.forward(httpRequest, httpResponse);
    }

}

At this point you might be wondering how the controller servlet would know to instantiate the appropriate Handler. The answer is simple. Two different HTTP requests cannot have the same URL. Hence you can be certain that the URL uniquely identifies each HTTP request on the server side and hence each URL needs a unique Handler. In simpler terms, there is a one-to-one mapping between the URL and the Handler class. This information is stored as key-value pairs in the properties file. The Controller Servlet loads the properties file on startup to find the appropriate Request Handler for each incoming URL request. 

The controller servlet uses Java Reflection to instantiate the Request Handler. However there must be some sort of commonality between the Request Handlers for the servlet to generically instantiate the Request Handler. The commonality is that all Request Handler classes implement a common interface. Let us call this common interface as Handler Interface. In its simplest form, the Handler Interface has one method say, execute(). The controller servlet reads the properties file to instantiate the Request Handler as shown in program above. 

The Controller Servlet instantiates the Request Handler in the doGet() method and invokes the execute() method on it using Java Reflection. The execute() method invokes appropriate business logic from the middle tier and then selects the next view to be presented to the user. The controller servlet forwards the request to the selected JSP view. All this happens in the doGet() method of the controller servlet. The doGet() method lifecycle never changes. What changes is the Request Handler's execute() method. You may not have realized it, but you just saw how Struts works in a nutshell! Struts is a controller servlet based configurable MVC framework that executes predefined methods in the handler objects. Instead of using a properties file like we did in this example, Struts uses XML to store more useful information.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Struts )

Latest Articles (in Struts)