Every so often good solutions are invented. At times they are discovered. Invention and discovery are not synonyms, 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
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. Illustrates what the first servlet looked like.
The goal of the application is to show how to access the property of an attribute and the property of an attribute that is itself a property of the attribute. You need to output the value of the name property of the Author. It is not possible to do this using JSP standard actions, as you will see later in this section, and in such cases, scriptlets have been used. This is how scripts lets found their way into JSP. However, you should not use script lets because with the scriptlets (Java code) intermingled in the presentation code (JSP); the business logic cross-cuts the presentation concern, resulting in unmaintainable JSP, as explained earlier. Because JSP standard actions cannot access properties of an attribute that is itself a property of an attribute and because using scriptlets results in unmaintainable JSPs, you should use EL. In this section, you will learn how to use EL with the help. You will access the name property of Author from Book without using scriptlets
Inside the Brackets
There can be one of the following inside the brackets of the [ ] operator:
- An index with or without quotes. A String literal.
- An EL implicit object or an attribute in any of the four scopes. The EL implicit object is explained.
- A nested expression. If there is an index with or without quotes inside the brackets of the[ ] operator, the variable to which the [ ] operator is applied is either an array or a list.
EL Implicit Objects
Scriptlets have access to several JSP implicit objects, as explained. These objects allow access to any variables that are held in the particular JSP scopes. EL also provides its own implicit objects called EL implicit objects. EL implicit objects are not the same as JSP implicit objects. All of these EL Implicit objects are Maps that map the respective scope attribute names to their values. For instance, using the implicit objects param and param Values, it is possible to access HTTP request parameters.
EL functions let you call the Java method from JSP without using scripting. An EL function is mapped to a static method of a Java class. This mapping is specified within a tag library descriptor (TLD), which is explained later in this section. Illustrates a simple Java method that returns the current date and time
Setting the Property of a Specified Target Object
To be able to set bean properties or map values using, we need to use target and property, instead of var, which will define the bean and property name to be set. If the target is a map, for instance, then the property is the name of the key and the value is the value for that key
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 in architecting enterprise systems. In this section, you will be introduced to the Java EE patterns in the web tier established by Alur, Crupi, and Marks.
Focused on evolving the technologies and tools used in a Java EE project. The chapter introduced you to the common problems associated with developing a web application and showed how these problems can be solved and how web applications can be made reusable, maintainable, and extensible by using tools (such as unified EL) available with Java EE.