Which Architecture to Use? - JSP

Now that you have seen an overview of the two architectures, how do you decide which approach to take? If you read some articles, you get the impression that the Model 1 architecture is the old or obsolete way, and that Model 2 is the new, better way. This is not the case, and the J2EE BluePrints back that up.

The Model 1 architecture is appropriate for Web applications that have the following characteristics:

  • Small applications in particular will benefit from this architecture.There is no need to go to the complexity of a controller servlet when there are only a handful of JSPs!
  • Applications that have a basic linking structure also lend themselves to the Model 1 architecture. If there is not much in the way of processing that needs to be done as the user navigates between pages, you might not benefit a great deal from having a controller. It can make it unnecessarily complex.
  • Applications that do not require security control can also be built using the Model 1 architecture. A controller can help manage security, but if there is no security to manage, this is irrelevant! The security of Web applications is discussed in more detail in Topic,“Security and JSP.”

The main drawback of the Model 1 architecture is that as sites built on this architecture grow and evolve, they can become very difficult to manage.Various pages are created with business logic within them, and debugging can become harder and harder as you end up with scriptlets in various locations containing small fragments of business logic. It is in these larger applications that the Model 2 architecture can really help.The main benefits of using the Model 2 architecture are as follows:

  • Having a controller makes the maintenance of larger sites more straightforward because you can manage the navigation centrally through the use of the controller servlet.
  • You can also manage security more easily with the Model 2 architecture because every request goes through the central controller servlet, which can check the user credentials against any requested resource.
  • Internationalization, although never straightforward, can be implemented more easily when using the Model 2 architecture. Presentation in different languages can be forwarded depending on the locale of the client.This will be discussed again later in this chapter when frameworks are discussed. For basic internationalization, you can use the i18n tags discussed in Chapter 5, in the section entitled,‘The Internationalization and Formatting Actions.’ n Multiclient device sites can be built more easily using the Model 2 approach because different views of the data can be presented depending on the client device type. So, a mobile device can be presented with one view, a browser with another, and so on.
  • It is easier to build the site in a modular way, and then unit-test the site in discrete functional blocks.This is not so easy with the Model 1 architecture because the various Beans are created and set up from different JSPs, and in a large site, it becomes increasingly difficult to modularize the application, and identify from which pages Beans are created and modified.

Although the Model 2 architecture has been introduced already with the sample shopping cart application, there are a number of substantial improvements that could be made.Take this section of the controller servlet for example:

This is only a simplistic example application.Imagine a real application with potentially hundreds of different views. Iwouldn’t want to be the person maintaining an if/else block for that! Also, there is no business logic associated with each view, but if there was, where would it go? In this example,it would need to be invoked somehow from the if/else block.

There has now emerged a number of architectural frameworks that add substantially to the basic Model 2 architecture that you have seen in the previous example.These frameworks will be discussed in the remaining pages of this chapter.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JSP Topics