Learn Java for Web Development

(Tina Meador) #1

146 CHAPTER 3: Best Practices in Java EE Web Development


Best-Practice Solutions: Using Patterns


While Java EE does a great job of standardizing the enterprise infrastructure, of providing an
application model, and of providing components adequate to develop web applications, interacting
directly with the Java EE components often results in massive amounts of boilerplate code and even
code redundancy. Using Java EE does not naturally lead to best-practice architecture and design.
To that end, Deepak Alur, John Crupi, and Dan Malks first established the Java EE design patterns
based on their experience of architecting enterprise systems. In this section, you will be introduced
to the Java EE patterns in the web tier established by Alur, Crupi, and Malks.


Note The book Core J2EE Patterns: Best Practices and Design Strategies by Alur, Crupi, and Malks (Prentice
Hall, 2003) is highly recommended for learning about best-practice architecture and design. However, there
have been substantial changes in the business and persistence JavaEE patterns with the Java EE 6 and Java
EE 7. For instance, some patterns such as Service Locator have been dropped in favor of the Dependency
Injection pattern. But the web tier patterns remain the same. You can find substantial literature on the new
business and persistence Java EE patterns in Real World Java EE Patterns: Rethinking Best Practices (second
edition) by Adam Bien.

The web tier encapsulates the presentation logic that is required to provide the service to the clients.
The presentation tier does the following:


   Intercepts the client requests
 Provides functionality such as authentication, authorization, encryption, and
session management, to name a few
 Accesses business services
 Constructs the response
 Renders the response to the client

In general, web application development requires you to address a common set of problems.


   When a request enters a web application, it often has to be preprocessed
to provide certain functionality such as authentication, authorization, and
encryption.
 The presentation tier and the business logic are often intermingled. This makes
the presentation tier difficult to maintain.
 Views are often encoded with the view navigation logic. This results in
intermingled view content and view navigation.
 There is no centralized component for view management, which results in code
redundancy and code scattering across the views.

This is not a complete list of problems, but these are the most common of them in a web application.
Fortunately, these problems in a web application can be addressed using web tier Java EE patterns.
How to use these Java EE patterns differs with each problem. Table 3-24 describes these patterns.

Free download pdf