MVC with configurable controller
By: Charles
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.
Archived Comments
1. This is very simple and easy to understand.
Thank you
View Tutorial By: satish at 2011-12-25 09:03:16
2. It is clear and understandable for beginners.
I got an clear idea of mvc and its operations.<
View Tutorial By: Archana at 2011-12-14 12:31:21
3. the example is not at all working though i remove some bugs occuring it raising one or the other pro
View Tutorial By: sanju at 2011-09-07 07:50:38
4. thank u,i was searching this one only
View Tutorial By: mithun at 2010-12-19 00:18:44
5. It is simple and clear.. Thnx.
View Tutorial By: kiru at 2010-04-06 01:48:04
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Configuring JDBC DataSources in Struts
When is the best time to validate input in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?
What is a Plug-in and how to use Java plug-ins with Struts?
Origin and Architecture of Struts
Handling multiple buttons in HTML Form in Struts