Using MVC in Designing Web Applications

A good strategy when designing your enterprise application is to have separate layers, each of which is unaffected by changes done on the other layers. The Model-View-Controller is a design pattern that is used to partition your Web applications into the following components (see Figure 7.8):

  • Model: A set of objects that represent the business logic of the application. This usually includes EJBs, JavaBeans, and other abstractions of real world entities.

  • View: Represented by JSPs as the user interface, which is necessary to construct the presentation. You can have multiple views for the same model.

  • Controller: Represented by servlets, which contain the necessary logic to process user events, manage screen flow, and select an appropriate response.

Figure 7.8. MVC design pattern for Web applications.


The main target here is that the model be kept separate from the details of how the application is structured (the controller) and how the information is presented to the user (the view).

The advantage of the MVC strategy is that there is no application logic within the presentation component itself (JSPs). It is simply responsible for retrieving any objects or JavaBeans that might have been previously created by the controller, and for generating the dynamic content for insertion within its static templates. The separation of the presentation logic from its content helps divide the roles and responsibilities between page designers and Java developers. Moreover, using the servlet as a controller presents a central point of entry to the application, which makes the management of applications more consistent and easier to maintain.

Working with a Servlet as a Controller

You learned earlier today how servlets work as delegates for the client tier to the business logic of the application. A primary role of a servlet is to act as a controller. A servlet is responsible for taking parameters from the client and then delegating those parameters to the appropriate components, which handle the business logic. Finally, the servlet takes the result back and uses it to provide a response to the user. The servlet usually forwards the response to a JSP to perform a presentation task (see Figure 7.9). Servlets are the link between the client request and the model (EJB and JavaBeans).

Figure 7.9. Servlet as a controller.


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

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