Back to article

Applying MVC to Web-Based Applications with Generic Views

December 2, 2003

The business world is heavily investing into the evolution of their services and product lines; both presentation and customer interaction are becoming Internet centric. Corporations are establishing a Web presence to attract new customers, diversify, and simplify its interaction with its existing customer base, ease business-to-business communications, or to simply put a new face on an old service. Today, practically every bank offers online banking; every financial institution, brokerage house, and hedge fund is rewriting their trading systems to allow global access and trading from any internet-enabled location. Airline, defense, pharmaceutical, document management, and other industries are all adding Web interfaces to their business models.

This rapid emergence of Web-based services and applications caused a tremendous growth in the Web application development area. Existing programming methodologies, design patterns, and code libraries have been reapplied (or rewritten) to make them pertinent to Web-based applications. Even entire frameworks have been created to decrease development time, ease maintenance cycles, and simplify coding of online applications. Because the Model-View-Controller (MVC) design paradigm's main purpose is to separate business logic from presentation and because it's flexible enough to be incorporated into any type of application, it was a prime candidate for adaptation in Web-based applications and services. Apache's software foundation project Struts is actually a framework implementation of MVC in Java, and the FuseBox project is another implementation of MVC in multiple Web technologies: ColdFusion, PHP, Java, ASP, and Lasso.

In my last article (Creating Dynamic Swing Views with Reflection by Extending MVC), I described how to apply MVC to a Java Swing application to dynamically generate JTabbedPane views by using Reflection and briefly described the history of the Model-View-Controller; in this article, I'll show how to use MVC in a Web-based project. My controller will rely on Reflection API to dynamically call action methods, redirect to proper views, and coordinate data flow between presentation and model layers.

Because it will be a Web-based project, I will use Servlets for server-side processing and controller implementation, Java Beans for the model layer, and JSPs as a presentation layer. If you are not familiar at all with Servlets, JSPs, and J2EE component development, please first read the referenced tutorials at the end of this article.

In order to test my project or if you want to use it as a backbone for a larger program, you will need to configure the J2EE compliant application server. I use the freely available Tomcat, but in an enterprise environment you will probably use IBM WebSphere or BEA WebLogic as commercial J2EE application servers with EJB and JSP containers. The setup of application server is beyond the scope of this article, but project's source is packaged as a J2EE—compliant application WAR (Web archive) file with all necessary XML deployment descriptor files; so, to run it on a configured server, all you'll need to do is drop it in an appropriate place. Also, if you feel like investing a bit more time and not writing your own Controller layer, you may want to look at the Apache Struts framework, which also comes with a huge JSP tag library for all sorts of functionalities you may need.


To show how to use MVC in a Web-based application, I've created a simple project consisting of several JSP Views—viewable in any web browser—several helper bean and action classes, and a Servlet controller class. The business objective will be to display weather information based on the user's ZIP code or city name. The project's structure is generic enough to make it easily modifiable for any type of larger online application. Enterprise-level applications usually have a database in the back end, to fetch dynamic data for the views, comprising a so-called three-tier architecture—client application, server processes, and enterprise data services; in my case, hoverer, all data info will be stored in a HashMap object.

Architecture Overview

Click here for a larger image.

Main Controller

The main controller of any MVC design is responsible for the coordination of data flow between the model and view layers, respond to user requests, and management of data in models with actions. This is why the Model-View-Controller can by applied to a Web-based application; if the controller is properly written, it will channel request data and invoke action calls for any type and number of views; therefore, you can easily apply the same controller to any project and add as many views as you need.

I called my controller Servlet "MainServlet" and added its definition into an XML deployment descriptor file under new Web application (webapp), called WeatherAppWeb in my application server.

<web-app id="WebApp">


The Servlet has standard doGet and doPost methods; but in addition, it has HashMap to store Action objects dynamically created based on keys passed from JSP views.

Instead of having a property file to match key with what action to perform or JSP to display, I put action keys into JSPs themselves as hidden variables indicating to the controller what to do next.

The main magic happens in the doPost method, when JSP submits its data to the Controller. I use Reflection API to instantiate the Action class (first I check whether it yet exists), and then call a method indicated by a parameter key. Action classes are used by the Controller to perform any sort of action on the user's behalf. All action classes implement an empty ActionInterface, so that they can be instantiated, using Reflection, based on parameter key passed from the JSP form. Action classes are used to populate helper Java beans or do any other task. After the Action call returns, I redirect to another view—based on the second key parameter in a finally clause.

Note: the Key notation is <Action Class name>.<action method name>—for example, WeatherAction.viewByZip—where WeatherAction is a class implementing ActionInterface and viewByZip is its method taking two objects (request and response) as parameters. See Listing 1.

JSP Views

The views are simply HTML pages that the user can see in any Web browser. They are generated by the Application Server from JSPs. Post views have an HTML form with a submit action pointing to the Controller Servlet.

<FORM action="../MainServlet" method="post">

and two key parameters indicating to the controller what to do and where to redirect.

<input type="hidden" name="ACTIONKEY" value="WeatherAction.viewByZip">
<input type="hidden" name="REDIRECTKEY" value="weather_data">

In my case, one view of JSP gets the ZIP code and submits it to the controller, which instantiates an Action class based on the first key—WeatherAction—and calls the viewByZip method, passing to it a request object. From the request object, I get all parameters and do a lookup by ZIP code in a Data Store WeatherData object. The reference to the result bean is put back into the request object. See Listing 1. After the method finishes, the Controller Servlet redirects to "weather_data.jsp", specified by the second key. The JSP that uses the data bean only has to call the proper methods in the right places in HTML to show dynamic data. See Listing 2.

As you can see from the architecture overview diagram above, I have two JSP views (one taking a ZIP code and another taking a City string), both having a second redirect key pointing to the third JSP (weather_data) were I show results. By following this pattern, you can add any number of Views and models with the correct key values, and the Controller will effortlessly coordinate data flow between them, without knowing anything about views or corresponding models.

One last thing to mention about JSP views is that I validate the ZIP and City fields by using JSP Tag library on the server-side added by:

<%@ taglib uri="/WEB-INF/validtag.tld" prefix="valTags" %>

If you are not familiar with JSP Tags, don't worry; I'll describe in detail how to use them for data verification in my next article, "Verifying User Input in MVC Views Using JSP Tag Libraries".

Java Bean Model Layer

In my Web MVC project, the model layer consists of helper beans that hold data. They only have instance fields, getter, and methods. They are located in the 'beans' package and are populated by Action classes. When the controller executes an action method, the action may populate a bean, or do some other operation. In an enterprise project, the action may do a lot more: Go to the RDBMS, mainframe, or any other data source to get data to populate bean, send data to data source, update data, do security checks, and other tasks. Actions are really Controller helper classes, but they also belong to the Model layer because they control the model data state and respond to user requests.


As you can see, MVC is ideally suited for Web-based application development. Once the controller is done, programmers can create many views and corresponding models (actions + beans) as the application use-case and spec requires. And, because the controller can be simply reused in a new application, development time shrinks and can be spent on writing business logic and functionalities represented in the model. Views can be created as a prototype, done entirely in HTML first and then converted to JPS or replaced by another technology altogether. If you have any questions or comments about this or any other of my articles, please e-mail me at

Code Listings

Listing 1

public void doPost(HttpServletRequest req,HttpServletResponse res)
  throws ServletException, IOException{
    String action = (String) req.getParameter("ACTIONKEY");
    String redirect = (String) req.getParameter("REDIRECTKEY");
    ActionInterface actionInterface = null;
    HttpSession session = req.getSession();

try {
  String classNameStr = action.substring(0, action.indexOf("."));
  String methodName = action.substring(action.indexOf(".") + 1);
  // if action exists - get it, 
  actionInterface = (ActionInterface) actions.get(classNameStr);
  // if null - instantiate it
    (actionInterface == null) {
    className = Class.forName("actions." + classNameStr);
    actionInterface = (ActionInterface) className.newInstance();
    actions.put(classNameStr, actionInterface);

    method = actionInterface.getClass().getMethod(methodName,
    Class[] { o.getClass(), o.getClass()});
    method.invoke(actionInterface, new
    Object[] { req, res });

    } catch (Exception e) {
    "Problem in MainController Servlet doPost\n" + e);
    "Problem in MainController Servlet doPost\n" + e);
    } finally {
    .getRequestDispatcher("/jsp/" + redirect + ".jsp")
    .include(req, res);

Listing 2

public synchronized void viewByZip(Object reqO, Object resO) {

  HttpServletRequest req = (HttpServletRequest) reqO;
  HttpServletResponse res = (HttpServletResponse) resO;
  String zip = (String) req.getParameter("ZIP");
  String data = (String) WeatherData.getData(zip);

  if (data == null)
  data = "Sorry; no weather data is available for zip:" + zip;
  // fill new bean
  WeatherBean wb = new WeatherBean();
  // put bean in to request user's object
  req.setAttribute("weather", wb);

Listing 3

<% beans.WeatherBean wb =
  (beans.WeatherBean)request.getAttribute("weather"); %>
<P>Weather information for <%=wb.getZip() %></P>
  <%=wb.getData() %>
<% if (session.getAttribute("msg") == null)
      { session.setAttribute("msg", "& "); } %>
<%=session.getAttribute("msg") %>
<% session.setAttribute("msg", "& "); %>
  <backTags:BackLink text="<- Back" />

Download source code here.

About the Author

Vlad Kofman is a System Architect currently working on projects under government defense contracts. He also has been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.


Java Servlet Technology by Stephanie Bodoff

Servlets and JavaServer Pages (JSP) 1.0: A Tutorial

Sun Reflection API by Dale Green

WebSphere Studio Application Developer Version 5 Programming Guide
By Ueli Wahli, Ian Brown, Fabio Ferraz, Maik Schumacher, and Henrik Sjostrand
IBM Redbook SG24-6957-00, May 9, 2003

Core Java 2, Volume II: Advanced Features (5th Edition)
by Cay Horstmann, Gary Cornell
Publisher: Prentice Hall; 5th edition (December 10, 2001)

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date