In this article I’ll show how to use MVC in a web-based project. My controller will rely on Reflection to dynamically call action methods and redirect to proper views, and coordinate data flow between presentation and model layers. Since, it will be a web-based project I will use Servlets for server-side processing and controller implementation, Java Beans for model layer and JSPs as presentation layer. If you are not familiar at all with Servlets, JSPs and J2EE component development, please first read referenced tutorials at the end of this article.

The business world is heavily investing into evolution of their services and product lines; both presentation and customer interaction are becoming internet centric. Corporations are establishing web presence to attract new customers, diversify and simplify their interaction with 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 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 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 FuseBox project is another implementation of MVC in multiple web technologies: ColdFusion, PHP, Java, ASP and Lasso.

In order to test my project or if you want to use is as a backbone for a larger program, you will need to configure J2EE compliant application server. I use freely available Tomcat, but in an enterprise environment you will probably use IBM WebSphere or BEA WebLogic as commercial J2EE application server with EJB and JSP containers. The setup of application server is beyond the scope of this article, but project’s source is packaged as J2EE compliant application WAR (web archive) file with all necessary xml deployment descriptor files, so in order 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 apache Struts framework, which also comes with a huge JSP tag library for all sorts of functionality you may need.

Project

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 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 so called 3-tier architecture - client application, server processes and enterprise data services; in my case, however, all data information will be stored in a HashMap object.

Architecture Overvie

Architecture Overview