| Simple J2EE Model View Controller Type II | | | | String strFile = this.getServletContext().getRealPath(" |
| Framework | | | | ings"); |
| Executive Summary | | | | System.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 applications | | | | and initialized the ActionBeanMapping helper class. |
| is the separation between the logics that deal with | | | | ActionBeanMapping.java:package simple;import |
| Presentation itself, the data to be presented and the | | | | java.io.FileInputStream;import |
| one that controls flow of logic. It is as an answer to | | | | java.io.IOException;import java.util.Properties;public |
| such concerns that the Model-View-Controller or MVC | | | | class ActionBeanMapping {private Properties prop = |
| pattern was designed. | | | | new Properties();public ActionBeanMapping(String |
| This paper provides the solution to modularize the | | | | propFile) throws IOException {this.prop.load(new |
| user interface functionality of a Web application so | | | | FileInputStream(propFile)); |
| that individual parts can be easily modified, that is | | | | }public Object getActionInstance(String action)throws |
| model view controller framework. | | | | Exception { |
| Introduction | | | | String strClass = |
| The Model-View-Controller (MVC) pattern separates | | | | prop.getProperty("action."+action.trim());if(strClass == |
| the modeling of the domain, the presentation, and | | | | null)throw new NullPointerException("Null |
| the actions based on user input into three separate | | | | action::"+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 for | | | | Exception { |
| information about its state (usually from the view), | | | | String strClass = |
| and responds to instructions to change state (usually | | | | prop.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 and | | | | Class.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 the | | | | This class reads the properties file and provides two |
| controller depend on the model. However, the model | | | | methods to instantiate the Action and Bean classes |
| depends on neither the view nor the controller. This | | | | using java reflection for the specified user action. |
| is one the key benefits of the separation. This | | | | The GET and POST methods of the request calls the |
| separation allows the model to be built and tested | | | | following code in controller.try { |
| independent of the visual presentation. The | | | | String strJsp = null; |
| separation between view and controller is secondary | | | | String strURI = request.getRequestURI();int |
| in many rich-client applications, and, in fact, many user | | | | startIndex = strURI.lastIndexOf("/");int |
| interface frameworks implement the roles as one | | | | endIndex=strURI.lastIndexOf(".do"); |
| object. In Web applications, on the other hand, the | | | | String strAction =strURI.substring(startIndex+1, |
| separation between view (the browser) and | | | | endIndex);this.populateBean(request, strAction); |
| controller (the server-side components handling the | | | | SimpleHandler handler = |
| HTTP request) is very well defined. | | | | rJsp = handler.process(request, |
| The solution provided in this paper is used very | | | | d(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 framework | | | | error.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 Types | | | | and instantiates the bean and action class and |
| MVC Type-I: In this type of implementation, the | | | | populates the model and invokes the method on an |
| View and the Controller exist as one entity -- the | | | | action class. All the actions classes in the application |
| View-Controller. In terms of implementation, in the | | | | should implement the interface SimpleHandler. If any |
| Page Centric approach the Controller logic is | | | | error 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 extracting | | | | SimpleHandler.java:package simple;import |
| HTTP request parameters, call the business logic | | | | javax.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 within | | | | SimpleHandler {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 of | | | | process(HttpServletRequest request, |
| maintainability. With Controller logic embedded within | | | | HttpServletResponse response) throws Exception; |
| the JSP using scriptlets, the code can get out of | | | | } |
| hand very easily. So to overcome the problems of | | | | All the action classes in the application should |
| maintainability and reusability, the Controller logic can | | | | implement the process method. |
| be moved into a servlet and the JSP can be used for | | | | Population 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 in | | | | Object obj;try {obj = |
| JSP in Type-I and in Type-II its moved to servlet. | | | | mapping.getBeanInstance(strAction); |
| MVC Type-II Framework | | | | Method 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 the | | | | arrayValue[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;import | | | | This method populates the model data and binds the |
| javax.servlet.http.HttpServlet;import | | | | model to request object, this model is accessed by |
| javax.servlet.http.HttpServletRequest;import | | | | the action class and JSP.error.jsp |
| javax.servlet.http.HttpServletResponse;public class | | | | [%@ page language="java" |
| SimpleController extends HttpServlet {private | | | | pageEncoding="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("/");int | | | | processing 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 { | | | | |