An Intro to JSP, JSF, and EL
Let's talk about accessing HTTP objects using JSP, the newer and preferred JSF, and how to use Expression Language in your Java EE projects.
Join the DZone community and get the full member experience.
Join For FreeIn this article, I am going to take a look at JavaServer Pages (JSP) and Expression Language (EL) and then relate it to JavaServer Faces (JSF). I will talk about how to access HTTP objects directly in JSP and JSF code, and you will see some examples of the syntactic difference between them.
JSP Is Legacy Technology
JSP is Java EE’s legacy web programming technology, which was released in the first version of J2EE back in 1999. Later it was replaced in 2003 by JSF, but its development continued with the latest version 2.3, released in Java EE 7. As of yet, it has not been deprecated.
JSF Is Preferred
Even though JSF has overtaken JSP as the preferred option, there are still many applications that use JSP, and it is very likely that you will come across such applications for quite a few years to come, so it’s worth having an appreciation of this technology.
Dynamic Java Web Applications
JSP is a server-side technology that allows a developer to create dynamic Java web application. JSP can be thought of as an extension to Servlet technology because it provides features to easily create user views. JavaServer Pages consists of HTML code but it allows Java code inclusions for dynamic content creation. Since web applications contain a lot of user screens, JSPs are used a lot in web applications.
Bridge the Gap Between Java and HTML
To bridge the gap between Java code and HTML in JSP, it provides additional features such as JSP Tags, Expression Language, and Custom Tags. This makes it easier to understand, and it helps a web developer quickly develop JSP pages. However, most of the time, we use JSP for view generation only and all the business logic is present in servlet code, Enterprise Java Beans, or model classes.
It is a much less sophisticated view rendering language compared with JSF and does not benefit from the advantage brought by components. However, the separation of view logic and business logic is not always kept so clear. JSP Scriptlets allow Java code to be written directly in the view logic. This clouds the separation.
Inline Java
Such Java code is entered directly in the JSP page between rocket and percentage <%…%>
Here, we are using Java code to access the HTTPServerRequest object in order to retrieve the query parameter named id and password.
Mixing this kind of logic with view technologies is bad practice. This is why modern Java EE applications opt not to use JSP but instead use the better structured component-based JSF language.
JSP Implicit Objects
JSP implicit objects are created by the servlet container while translating JSPs to Servlets. These are mainly related to HTTP objects and scopes. We can use implicit objects in JSP directly in scriptlets, as shown in the above code snippet, to access the values related to the current scope or HTTP objects.
In the following code snippet, we are referencing the HTTP request objects to obtain the context path.
<%=request.contextPath %>
Examples of other implicit JSP objects are request, response, pageContext, and application.
To complicate matters further, Expression Language has its own implicit objects that are similarly name to those available in JSP and relate to the same HTTP objects and scopes.
${request.contextPath}
Examples of other EL implicit objects: request, requestScoped, pageContext, applicationScoped
Here we are obtaining the context path from the HTTP request object, just as we did in the JSP example before. Note that some of the objects are named differently and different syntax is used.
Using EL in JSP and JSF
Let’s widen the topic slightly and look at how we use Expression Language in JSP and JSF.
The following code snippet shows the use of EL in a JSP:
- Implicit objects: ${request.contextPath}
- Bean property: ${book.title}
And the following code snippet shows that use of EL in a JSF:
- Implicit objects: #{request.contextPath}
- Bean property: #{book.title}
In both cases, the object reference is named the same and references the same object. The only difference is the syntax used to reference the instance. JSP uses the dollar sign while JSF uses the hash. The bean name is referenced by using the class name with the first letter in lowercase (unless another name has been explicitly defined in the named annotation).
And finally, let’s see just a little of the syntax that we use in Expression Language.
- Logical operators
- [], (), –, < = >, eq ne, || and more
- and, not, instanceof, true, mod and more
- ${not empty book.title}
As you might expect, it is very familiar. We have the standard logical operators that validate equality and perform mathematical operations. Additionally, we are given some syntactic sugar over compound operations such as the not empty operation we see here.
Further Reading
How about learning a little about Context and Dependency Injection (CDI) and Enterprise Java Beans (EJB)? These are two core technologies.
I have recently posted a mini-series of blogs taking a look at JAX-RS. They discuss how to manage bean validation failure, work with Consumers and Producers, and how to create JAX-RS Resource Entities.
There are two deep dive series on JAX-RS topics:
- What are JAX-RS annotations? A deep dive looking at the annotations most commonly used when developing REST endpoints.
- What is the @Context annotation used for? A deep dive into the many uses of the @Context annotation.
Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments