MVC with configurable controller

By: Charles Emailed: 1674 times Printed: 2157 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(4)


1. View Comment

It is simple and clear.. Thnx.

View Tutorial          By: kiru at 2010-04-06 01:48:04
2. View Comment

the example is not at all working though i remove some bugs occuring it raising one or the other problem..........
plz provide accurate code so that we can understand it better


View Tutorial          By: sanju at 2011-09-07 07:50:38
3. View Comment

It is clear and understandable for beginners.
I got an clear idea of mvc and its operations.

Thank u


View Tutorial          By: Archana at 2011-12-14 12:31:21
4. View Comment

This is very simple and easy to understand.

Thank you


View Tutorial          By: satish at 2011-12-25 09:03:16

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Charles
arraycopy example in Java
Action listeners in JSF
Automatically Including Preludes and Codas in JSP
Using realloc() Function in C
Handling multiple buttons in HTML Form in Struts
Fopen and Getc implementation program in C
Pointers to Structures example program in C
Pointers vs. Multi-dimensional Arrays in C
getch and ungetch in C
Bitwise Operators in C
Variables and Arithmetic Expressions in C
Differences between JavaServer Faces technology and Struts
assert() example program in C++
'double buffering' Sample program in J2ME
Converting Default Function Arguments in C++ to Java

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Struts 1 vs Struts 2
Handling Duplicate Form Submissions in Struts
What is Struts? Which Version of Struts to use?
ActionErrors and ActionError in Struts
ForwardAction in Struts
7 Best Practices of Struts
Simple example of using the requiredif Validator rule in Struts
Installing and configuring Tomcat and Struts
Handling multiple buttons in HTML Form in Struts
DispatchAction in Struts
Configuring JDBC DataSources in Struts
Introduction to Struts Architecture
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
Using JavaScript to submit a form in Struts
Most Emailed Articles (in Struts)
What is a Model View Controller (MVC) Model?
What is Struts? Which Version of Struts to use?
Is Struts the most popular web application framework for Java?
History of Struts
WebWork 2 is now Apache Struts 2
Struts Classes
MVC Architecture (Model 2 Architecture)
ActionErrors and ActionError in Struts
ForwardAction in Struts
Struts 1 vs Struts 2
Creating Struts Modules
Writing the first Struts application
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts
Handling Duplicate Form Submissions in Struts