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