Home arrow Java arrow Page 2 - J2EE Design Patterns: The Presentation Layer Patterns: Model-View-Controller
JAVA

J2EE Design Patterns: The Presentation Layer Patterns: Model-View-Controller


In any application, it is the presentation that matters most to the end user. The usability of software depends heavily on the User Interface or Presentation Layer in the case of J2EE. Since it is the Presentation Layer that presents the UI to the user, the solutions for recurring problems have to be standard. That is where Presentation Layer Design Patterns come into the picture.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
November 14, 2006
TABLE OF CONTENTS:
  1. · J2EE Design Patterns: The Presentation Layer Patterns: Model-View-Controller
  2. · MVC: the Types
  3. · MVC in the Real World
  4. · MVC in the Real World continued

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
J2EE Design Patterns: The Presentation Layer Patterns: Model-View-Controller - MVC: the Types
(Page 2 of 4 )

MVC can be implemented in two different ways which constitute the types of MVC. In technical terms the types are known as Models. Please do not confuse this Model with the Model (M) of MVC. Models of MVC represent the ways in which the MVC can be implemented whereas the Model represents data. On the basis of the different ways they are implemented, the two Models are MVC Model-I and MVC Model-II. The point of difference between them is where Controller is implemented. 

MVC Model-I

This Model is also known as the Page Centric Model. In this type of implementation, the View and the Controller exist as one entity -- the View-Controller. In terms of implementation, in the Page Centric approach the Controller logic is implemented within the View i.e. with J2EE, it is JSP. All the tasks of the Controller, such as extracting HTTP request parameters, call the business logic (implemented in JavaBeans, if not directly in the JSP), and handling of the HTTP session is embedded within JSP using scriptlets and JSP action tags. For example, to redirect to another page in the case of an invalid session, the MVC Model-I approach would be the following snippet within the JSP page:

<%

     if(session.getAttribute("USER_ID")==null)

       {

%>

    <jsp:forward page="/authenticator/login.jsp"/>

<%

   //other logic

%>

  Pictorially the MVC Model-I would be:

 

MVC Model-II

The problem with Model-I is its lack of maintainability. With Controller logic embedded within the JSP using scriptlets, the code can get out of hand very easily. So to overcome the problems of maintainability and reusability, the Controller logic can be moved into a servlet and the JSP can be used for what it is meant to be -- the View component. Hence, by embedding Controller logic within a servlet, the MVC Model-II Design Pattern can be implemented.

Thus the major difference between Model-I and Model-II is where the Controller logic is embedded, in JSP or in a servlet. To clarify Model-II a bit more, let's take the example used for Model-I and convert it to Model-II. Instead of embedding the code snippet in JSP, it would become a part of the servlet with necessary modifications as below:

if(session.getAttribute("USER_ID")==null) {

           

            RequestDispatcher dispatcher=request.getRequestDispatcher
("/authenticator/login.jsp");

            dispactcher.forward(request,response);

        }

        else {

      //other logic

}

     Pictorially the MVC Model-II would be:

   

That completes the second section of this discussion. In the next part I will be developing a mailing list application containing both Model-I and Model-II of MVC.


blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials