Tech News

Best Practices in Java EE Web Development

Every so often good solutions are invented. At times they are discovered. Invention and discovery are not synonyms,1 and they signify different objectives; however, both are realized through experience and expertise. Experience helps you achieve good solutions, and when you apply those good solutions to the same set of problems, patterns begin to emerge. Patterns are the catalog of good solutions that stem from the experience and expertise of developers.

n architect named Christopher Alexander observed that architects tend to solve the same problems in more or less the same way. This realization led him to write a book on design patterns for architects.2 He reflected in this book, “A design pattern describes a problem which occurs over and over again, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Applying these Java EE design patterns to the development of a Java EE–based application is de rigueur to achieving best-practice architecture and design. That said, ensuring best-practice architecture and design is not enough to ensure reusable, maintainable, extensible software. Even with the Java EE design patterns in place, a software project is often afflicted by a phenomenon called entropy, a measure of the degree of disorder. According to the Second Law of Thermodynamics, everything in the universe moves from low entropy (a state of order) to high entropy (disorder) and eventual chaos. Everything in nature is bound by this law of physics, and nature deals with this irrevocable law by means of evolution.

Best-Practice Solutions: Using EL and JSTL

Sun released the Servlet specification in 1998. The sole purpose of servlets was to help the Java web server generate dynamic content for the client. Servlets worked fine for a dynamic content generation but had one big problem. The view was hardwired into the servlet. To solve this problem, JSP was born. JSP removes the need of hardwiring the view code into the business logic code.

JSP standard actions are, in general, far too limited, and consequently, developers had to resort to using scriptlets to create function-rich web applications. Using Java code in the form of scriptlets in JSP leads to unmaintainable JSP pages. As a result, the JSP specification has evolved to support Java-free JSP pages. This support rests primarily on the JSP Expression Language (EL) and the JSP Standard Tag Library (JSTL). In the sections that follow, we will take a closer look at EL and JSTL.

Expression Language

Beauty without expression is lifeless, JSP without Expression Language is chaos. The underlying principle of the Expression Language is to provide scriptless JSP components. The Expression Language is used in two ways.

  • To retrieve objects from scoped attributes (explained in the previous chapter). These objects are JavaBeans, maps, arrays, and lists that have been stored as attributes in any of the four scopes
  • (also explained in the previous chapter). The EL searches the attributes first in the smallest scope, which is page scope; then in request and session; and finally in the largest scope, which is application scope.
  • To access request parameters, request headers, cookies, context-initialization parameters, and pageContext objects.

EL Operators

The EL operations are necessary to handle data manipulations. EL supports several operators such as relational, arithmetic, logical, and others.

Summary

EL and JSTL are two picture-perfect implementations of the theory of the separation of business and view concerns, and they can be used to build reusable, easy-to-maintain, function-rich web applications. This chapter also introduced you to the Java EE web tier patterns and how these JavaEE patterns provide solutions to the recurring problems in a particular context. Then the chapter showed how frameworks address architectural and business problem areas by providing patterns-based generic objects and supporting classes. Finally, the chapter showed how web frameworks relinquish you from using Java EE design patterns by providing you with out-of-the-box best-practice solutions.

Related Articles

Leave a Reply

Your email address will not be published.

Check Also
Close
Back to top button