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.
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";
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:
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.