Java
  Home arrow Java arrow Page 2 - J2EE Design Patterns: The Presentation Lay...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

J2EE Design Patterns: The Presentation Layer Patterns: Model-View-Controller
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2006-11-14

    Table of Contents:
  • J2EE Design Patterns: The Presentation Layer Patterns: Model-View-Controller
  • MVC: the Types
  • MVC in the Real World
  • MVC in the Real World continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More Java Articles
    More By A.P.Rajshekhar


       · MVC is the most used design pattern. In this article I have discussed about MVC....
       · example wont run because there´s no implementation of bean
       · First thank you for your comment. Secondly, the reason Bean implementation has not...
     

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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT