JSP - what's the difference between “<%

2019-05-03 02:09发布

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

What's the difference between both cases ?

Thanks

3条回答
迷人小祖宗
2楼-- · 2019-05-03 02:25
<%= 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;
    }
    
    %>
    
查看更多
Deceive 欺骗
3楼-- · 2019-05-03 02:27

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 ) %>.

叛逆
4楼-- · 2019-05-03 02:29

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

Here are the links to the jsp documentation:

查看更多
登录 后发表回答