Simple J2ee Model View Controller Type II Framework

Simple J2EE Model View Controller Type IIString strFile = this.getServletContext().getRealPath("
Frameworkings");
Executive SummarySystem.out.println("MAPPING FILE PATH::"+strFile);try
Application presents content to users in numerous{mapping = new ActionBeanMapping(strFile);
pages containing various data. Also, the engineering} catch (IOException e) {e.printStackTrace();
team responsible for designing, implementing, and}
maintaining the application is composed of individuals}
with different skill sets.Mapping file path is taken from the servlet config,
One of the major concerns with the web applicationsand initialized the ActionBeanMapping helper class.
is the separation between the logics that deal withActionBeanMapping.java:package simple;import
Presentation itself, the data to be presented and thejava.io.FileInputStream;import
one that controls flow of logic. It is as an answer tojava.io.IOException;import java.util.Properties;public
such concerns that the Model-View-Controller or MVCclass ActionBeanMapping {private Properties prop =
pattern was designed.new Properties();public ActionBeanMapping(String
This paper provides the solution to modularize thepropFile) throws IOException {this.prop.load(new
user interface functionality of a Web application soFileInputStream(propFile));
that individual parts can be easily modified, that is}public Object getActionInstance(String action)throws
model view controller framework.Exception {
IntroductionString strClass =
The Model-View-Controller (MVC) pattern separatesprop.getProperty("action."+action.trim());if(strClass ==
the modeling of the domain, the presentation, andnull)throw new NullPointerException("Null
the actions based on user input into three separateaction::"+action);return
classes.Class.forName(strClass).newInstance();
Model: The model manages the behavior and data of}public Object getBeanInstance(String action)throws
the application domain, responds to requests forException {
information about its state (usually from the view),String strClass =
and responds to instructions to change state (usuallyprop.getProperty("bean."+action);if(strClass == null)
from the controller).thrownew NullPointerException("Null
View: The view manages the display of information.bean::"+action);return
Controller: The controller interprets the mouse andClass.forName(strClass).newInstance();
keyboard inputs from the user, informing the model}
and/or the view to change as appropriate.}
It is important to note that both the view and theThis class reads the properties file and provides two
controller depend on the model. However, the modelmethods to instantiate the Action and Bean classes
depends on neither the view nor the controller. Thisusing java reflection for the specified user action.
is one the key benefits of the separation. ThisThe GET and POST methods of the request calls the
separation allows the model to be built and testedfollowing code in controller.try {
independent of the visual presentation. TheString strJsp = null;
separation between view and controller is secondaryString strURI = request.getRequestURI();int
in many rich-client applications, and, in fact, many userstartIndex = strURI.lastIndexOf("/");int
interface frameworks implement the roles as oneendIndex=strURI.lastIndexOf(".do");
object. In Web applications, on the other hand, theString strAction =strURI.substring(startIndex+1,
separation between view (the browser) andendIndex);this.populateBean(request, strAction);
controller (the server-side components handling theSimpleHandler handler =
HTTP request) is very well defined.rJsp = handler.process(request,
The solution provided in this paper is used veryd(request, response);
simple servlet and JSP and plain java objects, using} catch (Exception e)
this framework very easily any real time applications{e.printStackTrace();request.getRequestDispatcher("
can be developed. By following this simple frameworkerror.jsp").forward(request, response);
most of the complex MVC frameworks can be}
understood.This piece of code gets the user action from the URI
Model View Controller Typesand instantiates the bean and action class and
MVC Type-I: In this type of implementation, thepopulates the model and invokes the method on an
View and the Controller exist as one entity -- theaction class. All the actions classes in the application
View-Controller. In terms of implementation, in theshould implement the interface SimpleHandler. If any
Page Centric approach the Controller logic iserror occurs this controller forwards to a generalized
implemented within the View i.e. with J2EE, it is JSP.error page.
All the tasks of the Controller, such as extractingSimpleHandler.java:package simple;import
HTTP request parameters, call the business logicjavax.servlet.http.HttpServletRequest;import
(implemented in JavaBeans, if not directly in the JSP),javax.servlet.http.HttpServletResponse;public interface
and handling of the HTTP session is embedded withinSimpleHandler {public static final String BEAN =
JSP using scriptlets and JSP action tags."simple.BEAN";public String
MVC Type-II: The problem with Type-I is its lack ofprocess(HttpServletRequest request,
maintainability. With Controller logic embedded withinHttpServletResponse response) throws Exception;
the JSP using scriptlets, the code can get out of}
hand very easily. So to overcome the problems ofAll the action classes in the application should
maintainability and reusability, the Controller logic canimplement the process method.
be moved into a servlet and the JSP can be used forPopulation of model data from the request object is
what it is meant to be -- the View component.done by the following controller method.private void
Hence, by embedding Controller logic within a servlet,populateBean(HttpServletRequest request, String
the MVC Type-II Design Pattern can be implemented.strAction)
The major difference between MVC Type-I and{
Type-II is where the Controller logic is embedded inObject obj;try {obj =
JSP in Type-I and in Type-II its moved to servlet.mapping.getBeanInstance(strAction);
MVC Type-II FrameworkMethod methods[] =
In this frame work, Model is a plain old java object,obj.getClass().getMethods();for(int i=0; i0){strValue =
view is a JSP which will render the page using thearrayValue[0];
model , these two are application dependent and this}try {method.invoke(obj, strValue);
framework has a centralized controller is a servlet,} catch (Exception e) {e.printStackTrace();
which will populate the model and invokes a method}
from the action class.}
Below is the source of the controller.}request.setAttribute(SimpleHandler.BEAN, obj);
SimpleController.javapackage simple;import} catch (Exception e) {e.printStackTrace();
java.io.IOException;import}
java.lang.reflect.Method;import}
javax.servlet.ServletException;importThis method populates the model data and binds the
javax.servlet.http.HttpServlet;importmodel to request object, this model is accessed by
javax.servlet.http.HttpServletRequest;importthe action class and JSP.error.jsp
javax.servlet.http.HttpServletResponse;public class[%@ page language="java"
SimpleController extends HttpServlet {privatepageEncoding="ISO-8859-1"%]
ActionBeanMapping mapping;public void[html]
doGet(HttpServletRequest request,[head]
HttpServletResponse response)throws[title]Error page[/title]
ServletException, IOException {try {[/head]
String strJsp = null;[body]
String strURI = request.getRequestURI();int[font color="#ff0000"][b]Error occured while
startIndex = strURI.lastIndexOf("/");intprocessing request.[/b][/font]
endIndex=strURI.lastIndexOf(".do");[/body]
String strAction =strURI.substring(startIndex+1,[/html]
endIndex);this.populateBean(request, strAction);The web configuration is defined below, it’s a
SimpleHandler handler =simple configuration file for controller.web.xml:
rJsp = handler.process(request,[?xml version="1.0" encoding="UTF-8"?]
d(request, response);[web-app version="2.4"xmlns="
} catch (Exception e)[servlet]
{e.printStackTrace();request.getRequestDispatcher("[description]Simple J2EE Controller[/description]
error.jsp").forward(request, response);[display-name]Simple J2EE Controller[/display-name]
}[servlet-name]SimpleController[/servlet-name]
}public void doPost(HttpServletRequest request,[servlet-class]simple.SimpleController[/servlet-class]
HttpServletResponse response)throws[init-param]
ServletException, IOException {this.doGet(request,[param-name]actionmappings[/param-name]
response);[param-value]WEB-INF/actionmappings.properties[
}public void init() throws ServletException {param-value]
String strFile = this.getServletContext().getRealPath("[/init-param]
ings");[load-on-startup]1[/load-on-startup]
System.out.println("MAPPING FILE PATH::"+strFile);try[/servlet]
{mapping = new ActionBeanMapping(strFile);[servlet-mapping]
} catch (IOException e) {e.printStackTrace();[servlet-name]SimpleController[/servlet-name]
}[url-pattern]*.do[/url-pattern]
}private void populateBean(HttpServletRequest[/servlet-mapping]
request, String strAction)[welcome-file-list]
{[welcome-file]index.jsp[/welcome-file]
Object obj;try {obj =[/welcome-file-list]
mapping.getBeanInstance(strAction);[/web-app]
Method methods[] =The controller servlet is invoked for all the urls which
obj.getClass().getMethods();for(int i=0; i0){strValue =will ends with .do, this servlet loads on server startup,
arrayValue[0];and defines the action mappings file path.
}try {method.invoke(obj, strValue);Sample Application using the Framework
} catch (Exception e) {e.printStackTrace();Providing sample application to registration to store
}name, email and phone.index.jsp:
}[%@ page language="java"
}request.setAttribute(SimpleHandler.BEAN, obj);pageEncoding="ISO-8859-1"%]
} catch (Exception e) {e.printStackTrace();[html]
}[head]
}[title]Home page[/title]
}[/head]
The servlet’s init method is used to initialize the[body]
action and bean mappings.public void init() throws[form method="post" action="register.
ServletException {