JSP Expression Tag Syntax and Example



The JSP expression tag evaluates the expression placed in it, converts the result into String, and sends the result back to the client through response implicit object.
All JSP Examples at JSP Source Code Examples

1. The Syntax of JSP Expression Tag

<%= expression %>

2. JSP Expression Tag Examples

2.1 Convert String to Upper Case Example

Here is sample snippet which basically converts this string to all caps or to all upper case.
<html>
   <body>
        Converting a string to uppercase:
        <%=new String("Hello World").toUpperCase()%>
   </body>
</html>
Output:
Converting a string to uppercase: HELLO WORLD

2.2 Mathematical Expressions Example

<html>
    <body>
       25 multiplied by 4 equals 
       <%=25 * 4%>
   </body>
</html>
Output:
25 multiplied by 4 equals 100

2.3 Boolean Expressions Example

<html>
    <body>
        Is 75 less than 69?
        <%=75 < 69%>
    </body>
</html>
Output:
Is 75 less than 69? false

Complete Example with Output

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

     Converting a string to uppercase:
     <%=new String("Hello World").toUpperCase()%>
     <br />
     <br />
     Port of Server :
     <%= request.getLocalPort() %> 
     <br />
     <br />
     Context path :
     <%= application.getContextPath() %>
     <br />
     <br /> 
  25 multiplied by 4 equals
     <%=25 * 4%>
     <br />
     <br /> Is 75 less than 69?
     <%=75 < 69%>

</body>
</html>
Let's invoke this JSP page from a Web browser, you see the table on a browser:
All JSP Examples at JSP Source Code Examples

Comments