7

While working with JSP files and servlets , I came across <% … %> and <%= … %> .

What's the difference between both cases ?

Thanks

jmj
  • 237,923
  • 42
  • 401
  • 438
JAN
  • 21,236
  • 66
  • 181
  • 318

3 Answers3

13

<%= … %> will echo out a variable, where as <% … %> denotes a script or some code that is executed.

Here are the links to the jsp documentation:

poussma
  • 7,033
  • 3
  • 43
  • 68
Cristian
  • 700
  • 7
  • 9
11
<%= new java.util.Date() %> 

is same as

<% out.println(new java.util.Date()) %>

There are three types of Scriptlets :

  • Scriptlet Expressions of the form <%= expression %> that are evaluated and inserted into the output
  • Scriptlet of the form <% code %> that are inserted into the servlet's service method
  • Scriptlet Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods. For ex:

    <%!
    
    public int sum(int a, int b) {
    
    return a + b;
    }
    
    %>
    
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
7

In case of <% ... %> you are adding a server side code. And in case of <%= ... %> you are adding a server side code that automatically prints something. It could be seen as a shortcut for <% out.print( something ) %>.

Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106