Mohsin Shaikh
Mohsin Shaikh

Reputation: 504

In JSP Why Expression tag can not take the semicolon at end of statement

Expression element in the JSP <%= … %> Only one java expression Ex:

 Index.jsp
 ----------
 Welcome to JSP scripting elements

  <%! int num1=10;
      int num2=20;
      int add;
   %> 

<% add=num1+num2 %>

   Addition is<%= add %> <@!-- Expression tag -->

Upvotes: 3

Views: 2928

Answers (3)

Naveen Kumar Alone
Naveen Kumar Alone

Reputation: 7678

From the Java Server Programming Black Book:

An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file.

Like:

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

You cannot use a semicolon to end an expression.

In your example code

<%= add %>

means

out.print(add);

if you have to add semicolon before close tag

then it is invalid

out.print(add;);

It shows some error.

know more about JSP scripting tags

Upvotes: 2

Jayesh
Jayesh

Reputation: 6111

Because whatever is present after "=" in <%= %> that will be kept inside like out.print(abc);

So if you add semicolon, it will be like out.print(abc;); -> which is compile time error.

it is very similar to

without semicolon

System.out.println(abc);

and if you add semicolon then

System.out.println(abc;);

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691973

Because they're expressions, and not statements.

<%= add %>

is translated to

out.print(add);

So you really don't want a semicolon after the expression. It would lead to

out.print(add;);

which would not be valid Java.

Upvotes: 4

Related Questions