October 27, 2016
Hot Topics:

Applying MVC to Web-Based Applications with Generic Views

  • December 2, 2003
  • By Vlad Kofman
  • Send Email »
  • More Articles »

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 vlad_kofman@yahoo.com.

Code Listings

Listing 1

public void

 doPost(HttpServletRequest req,HttpServletResponse res)


 ServletException, IOException{


    String action = (String) req.getParameter(


    String redirect = (String) req.getParameter(


    ActionInterface actionInterface = null;
    HttpSession session = req.getSession();


  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

if (actionInterface == null) { className = Class.forName("actions." + classNameStr); actionInterface = (ActionInterface) className.newInstance(); actions.put(classNameStr, actionInterface); } method = actionInterface.getClass().getMethod(methodName, new Class[] { o.getClass(), o.getClass()}); method.invoke(actionInterface, new Object[] { req, res }); } catch (Exception e) { session.setAttribute( "msg", "Problem in MainController Servlet doPost\n" + e); System.out.println( "Problem in MainController Servlet doPost\n" + e); } finally { this .getServletContext() .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);



 (data == 


  data = "Sorry; no weather data is available for zip:" + zip;
  // fill new bean
  WeatherBean wb = 


  // 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

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)

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

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