DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Dependency Injection in Spring
  • A Systematic Approach for Java Software Upgrades
  • Java EE 6 Pet Catalog with GlassFish and MySQL
  • How To Check for JSON Insecure Deserialization (JID) Attacks With Java

Trending

  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • How to Convert Between PDF and TIFF in Java
  • AI, ML, and Data Science: Shaping the Future of Automation
  1. DZone
  2. Coding
  3. Java
  4. An Intro to JSP, JSF, and EL

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.

By 
Alex Theedom user avatar
Alex Theedom
·
Updated Nov. 24, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
14.0K Views

Join the DZone community and get the full member experience.

Join For Free

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

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.

${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.
Object (computer science) Java (programming language) application Spring Framework Java EE

Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Dependency Injection in Spring
  • A Systematic Approach for Java Software Upgrades
  • Java EE 6 Pet Catalog with GlassFish and MySQL
  • How To Check for JSON Insecure Deserialization (JID) Attacks With Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!