Using Servlets and JSPs Together: Model View Controller

J2EE includes a notion of the Model View Controller (MVC) pattern. This design pattern shows how multiple components can cooperate inside of the scope of a single J2EE application. In fact, the MVC pattern is not new. Developers using Smalltalk, a language that was one of the early object-oriented languages in computing, invented it. The pattern focuses on decoupling the graphical interface of an application from the code that actually does the work. This pattern turns out to be a very important idea that affects how code is built. MVC applies to other object-oriented languages, including Java.

MVC Components

There are three components in the MVC pattern in a typical J2EE Web application:

  • Model encapsulates the application data. Typically, the model is the relational database or EJB. But, this can be any component that is used to access the data (or “state”). In MVC, there exists only one model while there are multiple views and controllers.

  • View renders the interface to the user. Typically, the view is a JSP page. However, it also can be an application GUI as well.

  • Controller receives user actions and makes changes to the application state (data) accordingly. Typically, this is a servlet in a J2EE Web application.

MVC in Web Applications

A very common case in J2EE Web applications is to use servlets to do the form processing before forwarding control to the JSPs to do the HTML generation. In addition, the servlets interact with the business logic encapsulated in EJBs or elsewhere. Typically, a controller exists for each area of functionality of a Web application. So, a controller might exist for checking the balance in a user's account while another exists for transferring money. A single model might support multiple sets of controllers. For example, a set of controllers might exist for bank employees, while another exists for customers.

For each controller, JSP pages generate the user interface. These JSP pages typically include forms, which POST data to the appropriate servlet. Security and localized content can be applied by creating different JSP pages for each individual view. For example, a set of JSP pages might exist for bank customers in Japan while another set exists for American bank customers viewing U.S. bank accounts. Each JSP page would take the existing money values in an account and display that appropriately for the audience, while sharing the same controller and model.

MVC Applicability

MVC is most applicable as a design pattern when:

  • You are building a Web application that has very complex user interaction models. For example, it is very difficult to parse the results of HTTP POSTs in the context of a simple JavaBean in a JSP page. Or, if several different forms can post to the same page, or the same form can post to various different pages. The MVC pattern can enable you to avoid having to embed the logic for handling the query parameters in many different places.

  • You are building a Web application that is internationalized with many different views.

MVC is not applicable in some cases, such as simple applications with simple user interaction models. In any case, MVC can be considered as a design pattern when building your own Web application using servlets and JSPs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.116.233