JSP Expression Language

 JSP Expression Language (EL) is a powerful feature in JavaServer Pages (JSP) that allows you to access and manipulate data in a more concise and readable way. EL allows you to access JavaBeans properties, request parameters, session attributes, and other objects, all without needing to write Java code in the JSP page.

Basic Syntax of Expression Language

  • Expressions: EL expressions are enclosed in ${}. These expressions are evaluated and replaced with the corresponding values when the page is rendered.
${expression}

Examples of JSP Expression Language (EL)

1. Displaying a Simple String

You can directly display a string by using EL.

jsp
<p>${"Hello, World!"}</p>

This will output:

html
<p>Hello, World!</p>

2. Accessing Request Parameters

You can access request parameters (such as data from a form submission) in EL.

jsp
<form action="greeting.jsp" method="get"> <input type="text" name="name" /> <input type="submit" value="Submit" /> </form> <!-- greeting.jsp --> <p>Hello, ${param.name}!</p>

If the user submits the form with the value "John", the output will be:

html
<p>Hello, John!</p>

3. Accessing Session Attributes

You can access session attributes using EL. For example, assume you have an attribute in the session:

jsp
<% session.setAttribute("username", "Alice"); %> <p>Welcome, ${sessionScope.username}!</p>

This will display:

html
<p>Welcome, Alice!</p>

4. Accessing Application (Context) Attributes

Similar to session attributes, you can access application attributes using the applicationScope keyword.

jsp
<% application.setAttribute("appName", "My Web Application"); %> <p>Welcome to ${applicationScope.appName}!</p>

This will display:

html
<p>Welcome to My Web Application!</p>

5. Accessing JavaBeans Properties

You can access properties of JavaBeans using EL. Let's assume you have a User bean with a name property.

java

public class User { private String name; // Getter and Setter for 'name' public String getName() { return name; } public void setName(String name) { this.name = name; } }

In your JSP, you can access the name property of the User bean as follows:

jsp

<jsp:useBean id="user" class="com.example.User" scope="session" /> <jsp:setProperty name="user" property="name" value="John" /> <p>Hello, ${user.name}!</p>

This will display:

html

<p>Hello, John!</p>

6. Accessing Request Attributes

Request attributes (often set by a servlet) can be accessed using requestScope.

jsp
<% request.setAttribute("message", "Welcome to the site!"); %> <p>${requestScope.message}</p>

This will display:

html

<p>Welcome to the site!</p>

7. Mathematical Operations

You can perform basic mathematical operations in EL.

jsp
<p>${5 + 3}</p> <p>${10 * 2}</p> <p>${20 / 4}</p>

This will output:

html
<p>8</p> <p>20</p> <p>5</p>

8. Logical Operations

EL supports logical operators like ==, !=, >, <, &&, ||, etc.

jsp

<c:if test="${user.age >= 18}"> <p>You are an adult!</p> </c:if>

9. Using empty to Check for Empty Values

You can use empty to check if a value is empty, null, or an empty collection.

jsp
<p>${empty user.name ? 'No name provided' : user.name}</p>

If user.name is null or empty, it will output:

html
<p>No name provided</p>

10. Calling Functions in EL

You can also use built-in functions in EL (such as fn:length, fn:toUpperCase, etc.) if the JSTL functions library is available.

jsp
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <p>The length of the string '${user.name}' is: ${fn:length(user.name)}</p>

If user.name is "Alice", the output will be:

html
<p>The length of the string 'Alice' is: 5</p>

Conclusion

JSP Expression Language provides a simple, powerful way to interact with JavaBeans, request, session, and application attributes, perform mathematical and logical operations, and format data without the need to write Java code directly in JSP pages. It helps to make JSP pages cleaner, more readable, and maintainable.

Comments