JSP Expression Language Syntax





Hey guys in this article, you will learn about the JSP Expression Language Syntax with easy to understand examples

Read More:

Overview


  • JSP Expressions allows us to include Java code inside the JSP file.
  • JSP will process the Java code on the server and return the HTML content
Element Syntax
JSP Expression <%= //Java code %>
JSP Scriptlet <% //Java code %>
JSP Declaration <%! //Java code %>

 Examples


Following are some of the examples for Java expression

<p>Current date and time is <%= new Date(System.currentTimeMillis()) %> </p>

This will display the following HTML content

Current date and time is Fri Oct 08 13:47:29 IST 2021

We can even compare the 2 values.

<p>Is 5 greater than to 10? <%= 5 > 10 %> </p>

This will display the following HTML content

Is 5 greater than to 10? false

We can even write the mathematical expressions

<p>The sum of 2 number is <%= 5 + 5 %> </p>

This will display the following content

The sum of 2 number is 10

Complete Example


Follow the below steps to understand the complete example

Create a Project


You can check the following post to create a new Dynamic We Project

Create a JSP file


Create JSPExpressions.jsp file under WebContent and add the following content

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	<p>Current date and time is <%= new Date(System.currentTimeMillis()) %> </p>
	
	<p>Is 5 greater than to 10? <%= 5 > 10 %> </p>
	
	<p>The sum of 2 number is <%= 5 + 5 %> </p>
</body>
</html>

Run the file


Right click on the File, choose Run As, choose Run on Server. It will open in a default web browser, you will see the following content

00-08

That’s it for this post, if you like this post, please share this post with your friends and collogues and also share this on your social media profiles.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply