News Ticker

JSP, JSF and EL Introduction

JSP, JSF, Expression Language

JavaServer Pages, JavaServer Faces, and Expression Language

In 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 the 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 yet it has not been depreciated.

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 Application

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 to 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  <%…%>

JSP Code Example

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.


Examples of other EL implicit objects: requestrequestScoped, 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 Next?

If you are new to Java EE it can be overwhelming to get your head around all the APIs the form the enterprise eco-system. That is why I wrote and recorded the video training course Learning Java Enterprise Edition. It is a two-hour course that introduces you to all the most important Java EE APIs. With plenty of demonstrations, code examples, and practice tasks on how to program with Enterprise Java, you will up to speak and well on your way to being a Java EE developer.

Advance Your Knowledge

If you want to learn more, there are courses that dive deeper into each of the APIs. There is a course about the JAX-RS API in you advance your knowledge by learning how to construct RESTful endpoints. There is a course on the WebSocket API where you can learn how to develop a chat application and there is a course about JSON where you learn how to master the JSON-Processing API course. There are many courses on the horizon, so why not jump in now and give your Java EE career a kick.

1 Trackback / Pingback

  1. Eclipse MicroProfile: 5 Things You Need to Know

Leave a Reply

%d bloggers like this: