Tuesday, May 24, 2011

Understanding how to implement Model view Control pattern using J2EE and various Java frameworks

MVC stands for Model View Controller design pattern and is one of the widely used pattern especially in Web applications
The pattern segregates the presentation or UI layer as View layer, business logic or application processing into Controller layer and Model is the related data to be used for processing

Since it segregates each of the above three parts it becomes easy to replace one of the above component without affecting the other part and allows the application to replace the existing
component with advanced technology /tool/framework

You can watch my videos on MVC, Struts 2, Spring and other J2EE design patterns and frameworks

In this article we will see following topics

  • Implementing MVC using JSP/Servlet

  • Implementing MVC using Struts 2 framework

  • (Implementing MVC using Struts 2 framework will be part of next series

We will each of the above implementation in a “Hello name” example which takes name from the user and the displays a Hello name message

Integrated Development environment used is Eclipse Europa and Container used is Apache tomcat 6

Implementing MVC using JSP/Servlet

In case of MVC using JSP/Servlet following are the roles taken by each of the component involved

Step 1: Click New=>Other=>Web=>Dynamic web project

Step 2: Enter Project name as MVC and Click on Finish button

Step 3: Click on src and click on New=>package

Step 4: Enter name of package as com.questpond.controller

Similarly create one more package named com.questpond.model

Step 5: Right click Web Content folder New=>JSP

Enter the name of the jsp as index.jsp

Click on Finish button

Step 6: Enter the following code in index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index jsp</title>
<form action="Action.do" method="get">
Enter name <input type="text" name="name"><br/>
<input type="submit" value="submit"/>

Step 7: Right click on com.questpond.model click on New=>Class. Name the class as ModelBean

Step 8: Within the ModelBean create a variable welcomeMsg and write setters and getters for same

String welcomeMsg;
public String getWelcomeMsg() {
return welcomeMsg;
public void setWelcomeMsg(String welcomeMsg) {
this.welcomeMsg = "Welcome "+welcomeMsg;

Step 9: Right click on com.questpond.controller and create New=>Servlet

Write the name of servlet as ActionServlet

Step 10: Write the following code within doGet method of ActionServlet

String name =request.getParameter("name");
ModelBean model = new ModelBean();
request.setAttribute("welcomeMsg", model.getWelcomeMsg());
RequestDispatcher dispatch = request.getRequestDispatcher("welcome.jsp");
dispatch.forward(request, response);

Step 11: Create a JSP named welcome.jsp following Step 5 and put the following code inside jsp

<%=request.getAttribute ("welcomeMsg").toString() %>

Step 12: Right click on the project and click on export option (Export=>WAR file)

Step 13: Point the destination to the webapps folder of tomcat and check all the options present

Click on finish button

Step 14: Start tomcat (Tomcat=>start.bat) and put the following URL in web browser

Following page will appear

Step 15: Enter any name for e.g. John and click on submit button

A page with welcome message will appear as follows

MVC using Struts 2:

Struts 2 makes use of Intercepting Filter pattern which makes use of Filters which can be used for pre processing before executing controller as well as post processing after controller

For doing pre processing as well as post processing Struts 2 uses Interceptors. Thus request in this case comes to interceptors which then invokes action and based on result of action execution
corresponding view will be displayed to user. This configuration of view with result returned by action is done in an xml known as struts.xml

Model in this case can be Java bean class which can be used with Action

In case of MVC using Struts 2 following are the roles taken by each of the component involved

Advantage of struts 2 MVC is that view for particular action is configured in xml (struts.xml or other xmls included within struts.xml) and hence there is no direct dependency between controller and view

Step 1: Click New=>Other=>Web=>Dynamic web project

Step 2: Enter Project name as WelcomeStruts2MVC and Click on Finish button

Step 3: Copy following jars from the struts 2 libraries from distribution to WEB-INF/lib folder

• commons-logging-1.0.4.jar

• freemarker-2.3.8.jar

• ognl-2.6.11.jar

• struts2-core-2.0.14.jar

• xwork-2.0.7.jar

Step 4: Make following filter entry in web.xml

Note: In higher version of struts 2 we can use
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter for filter-class

Step 5: Create an XML within src folder and name it struts.xml

Step 6: Put the following lines inside struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="welcomeAction" class="com.questpond.action.WelcomeAction">
<result name="success">welcome.jsp</result>

Step 7: Create a new package by using New=>package and give name as com.questpond.action

Step 8: Click on the package and create a new class by using

New => Class and name the class as WelcomeAction

Step 9: Write the following code within the above class

String name;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public String execute(){
return Action.SUCCESS; }

Step 10: Create a new jsp by using New=>JSP and name it index.jsp and put the following code within the jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
<s:form action="/welcomeAction.action">
<s:textfield name="name" label="Enter name"></s:textfield>

Step 11: Create one more jsp with name as welcome.jsp and put the following code

<%@ taglib prefix="s" uri="/struts-tags"%>
Welcome <s:property value="name"/>

Step 12: Export the project as war by using right clicking the project and clicking on Export option

Fill the details like destination (where the war has to be kept) in the window. Check all other options and Click on Finish button

Step 13: Start apache tomcat and put the following URL in web browser

The following page will appear and fill the name with any string value

Step 14: After filling details click on submit button on which following page will be displayed

What’s next

In my next series we will see how to implement MVC using Spring framework

No comments: