JSP - what's the difference between “<% … %>” VS “<%= … %>”

孤者浪人 提交于 2020-01-02 03:55:10

问题


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

What's the difference between both cases ?

Thanks


回答1:


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

Here are the links to the jsp documentation:

  • Expression (<%= … %>) : http://java.sun.com/products/jsp/tags/11/syntaxref11.fm4.html
  • Scriptlet (<% … %>) : http://java.sun.com/products/jsp/tags/11/syntaxref11.fm5.html



回答2:


<%= 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;
    }
    
    %>
    



回答3:


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



来源:https://stackoverflow.com/questions/11481854/jsp-whats-the-difference-between-vs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!