Show List

JSP Expression Language

JSP EL (Expression Language) is a language used in JSP pages to simplify the process of accessing data and performing operations on that data. EL expressions are enclosed in ${...} and can be used to evaluate variables, perform arithmetic and logical operations, access objects and properties, and more. Here are some examples of how JSP EL can be used in JSP pages:

  • Accessing variables:
jsp
Copy code
<p>Welcome ${user.name}!</p>

In this example, the user.name expression is used to access the name property of the user object. The resulting value is then included in the output.

  • Performing arithmetic and logical operations:
jsp
Copy code
<p>The result of 2 + 2 is ${2 + 2}.</p> <p>Is 5 greater than 3? ${5 > 3}</p>

In these examples, arithmetic and logical operations are performed inside the ${...} expression. The resulting value is then included in the output.

  • Accessing objects and properties:
jsp
Copy code
<c:forEach items="${myList}" var="item"> <p>${item.name} - ${item.price}</p> </c:forEach>

In this example, the c:forEach tag is used to loop through a collection of data stored in the myList variable. For each item in the list, the var attribute is used to define a variable called item that represents the current item being processed. Inside the body of the tag, EL expressions are used to access the name and price properties of the item object.

  • Calling methods:
jsp
Copy code
<p>The current date is ${java.time.LocalDateTime.now()}</p>

In this example, the java.time.LocalDateTime.now() expression is used to call the now() method of the LocalDateTime class. The resulting value is then included in the output.

These are just a few examples of how JSP EL can be used in JSP pages. EL expressions can be used in a variety of contexts, including conditional statements, function calls, and more. Using EL can make JSP pages more concise and easier to maintain.


    Leave a Comment


  • captcha text