Picking the Right Component Type for Each Task

The Project Billboard application introduced in Chapter 12 is a fairly complex application. Half the pages are pure controller and business logic processing, it accesses a database to authenticate users, and most pages require access control. In real life, it would likely contain even more pages, for instance, pages for access to a shared document archive, time schedules, and a set of pages for administration. As the application evolves, it may become hard to maintain as a pure JSP application. It’s easy to forget to include the access control code in new pages, for instance.

This is clearly an application that can benefit from using a combination of JSP pages and the component types defined by the servlet specification for the MVC roles. Let’s look at the main requirements and see how we can map them to appropriate component types:

  • Database access should be abstracted, to avoid knowledge of a specific data schema or database engine in more than one part of the application: beans in the role of Model can be used to accomplish this.

  • The database access beans must be made available to all other parts of the application when it starts: an application lifecycle event listener is the perfect component type for this task.

  • Only authenticated users must be allowed to use the application: a filter can perform access control to satisfy this requirement.

  • Request processing is best done with Java code: a servlet, acting as the Controller, ...

Get JavaServer Pages, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.