Home arrow Java arrow Page 4 - 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 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.

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