Expression in JSP

Expression: A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client. When the scripting language is the Java programming language, an expression is transformed into a statement that converts the value of the expression into a String object and inserts it into the implicit out object.

The expression element can contain any expression that is a valid page scripting language. When the Java programming language is the scripting language you do not use a semicolon to end the expression. However, the same expression within a scriptlet requires the semicolon.

You can sometimes use expressions as attribute values in JSP elements. An expression can be complex and composed of more than one part or expression. The parts of an expression are evaluated in left-to-right order.

JSP Expressions are designed to output the value of a single Java variable or expression. They can be likened to JavaScript’s eval() function except that a JSP expression outputs the result into the content of the HTML file.

<html>

<body>

<%= 5+5 %>

</body>

</html>

 

This extremely abbreviated example demonstrates a JSP expression that has been inserted into an HTML page. The contents of the expression, which is in this case the math problem “5+5” are evaluated at runtime and inserted into the HTML that is delivered to the client side. So the web user who views the JSP webpage will see:

<html>

<body>

10

</body>

</html>

Obviously this is a very contrived and rather pointless example. However the true power of JSP becomes more obvious when you output the value of variables or class methods:

<html>

<body>

<%= (new java.util.Date()) %>

</body>

</html>

 

JSP Syntax-

 

<%= expression %>

 

Leave a Reply

Your email address will not be published. Required fields are marked *