Java
  Home arrow Java arrow Page 4 - 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 in the Real World continued


    (Page 4 of 4 )

    Now comes the Controller which is implemented as a servlet. It creates an instance of the MailingBean, sets the data, and calls the method for persisting the data. If the call is successful, it sets an attribute in the request scope specifying that the operation is successful; otherwise the attribute specifies that the operation is a failure. Then the request is redirected to the View.

    import javax.servlet.*;

    import javax.servlet.http.*;

    import java.io.IOException;

    public class ListController extends HttpServlet {

        public static final String FIRST_PARAM = "first";

        public static final String LAST_PARAM = "last";

        public static final String EMAIL_PARAM = "email";

        public static final String MAILINGBEAN_ATTR = "mailingbean";

        public void init(ServletConfig config)

        throws ServletException {

            super.init(config);

        }

        public void destroy(  ) {

        }

        // handle get requests   

        protected void doGet(HttpServletRequest request,

                             HttpServletResponse response)

        throws ServletException, IOException {

            // read the parameters from the request

            String first = request.getParameter(FIRST_PARAM);

            String last = request.getParameter(LAST_PARAM);

            String email = request.getParameter(EMAIL_PARAM);

            // get the mailing list bean for this list

            MailingBean mb = MailingBeanFactory.newInstance(  );

            // set the parameters into the bean

            mb.setFirst(first);

            mb.setLast(last);

            mb.setEmail(email);

            // perform the business method

            boolean result = mb.doSubscribe(  );

            // choose a page based on the result

            String op = "success";

            if (!result) op = "failure";

           

            String nextPage="/redirect.jsp";

            // store the operation state in the request context

            request.setAttribute(MAILINGBEAN_ATTR, op);

            RequestDispatcher dispatcher =

               getServletContext(  ).getRequestDispatcher(nextPage);

            dispatcher.forward(request, response);       

        }

    }

    Next comes the JSP. It checks the attribute and redirects accordingly. Here the Controller functionality has been divided into a servlet as well as the following JSP:

    <%@page contentType="text/html"%>

    <jsp:useBean id="mailingbean" scope="request"

                 class="MailingBean" />

    <html>

    <head><title>Subscription Results</title></head>

    <body>

    <br><br>

    <% if   

        ((String)request.getAttribute
    (MAILINGBEAN_ATTR).equalsIgnoreCase

                  ("success"))

    <%{%>

    <jsp:forward page="/success.jsp"/>

    <%}

    else

    {

    %>

    <jsp:forward page="/failure.jsp"/>

    <%

    }

    %>

    </body>

    </html>

    Here is the success.jsp:

    <%@page contentType="text/html"%>

    <jsp:useBean id="mailingbean" scope="request"

                 class="MailingBean" />

    <html>

    <head><title>Subscription Results</title></head>

    <body>

    <br><br>

    Dear <jsp:getProperty name="mailingbean" property="first"/>,

    <br><br>

    Congrats, the address

    <jsp:getProperty name="mailingbean" property="email"/>

    Has been added to the list.<br><br>

    </body>

    </html>

    And here is the failure.jsp:

    <%@page contentType="text/html"%>
    <jsp:useBean id="mailingbean" scope="request" 
                 class="MailingBean" />
    <html>
    <head><title>Subscription Results</title></head>
    <body>
    <br><br>
    Dear <jsp:getProperty name="mailingbean" property="first"/>,
    <br><br>
    We're sorry, the address
    <jsp:getProperty name="mailingbean" property="email"/>
    could not be added to the list.<br><br>
    The problem was: 
    <jsp:getProperty name="mailingbean" property="errorString"/>.
    </body>

    </html>

    That completes the application. Though MVC  provides maintainable code, there are still situations in which MVC has limitations. These cases and their solutions will be the focus of next part. Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 3 Hosted by Hostway
    Stay green...Green IT