简体   繁体   English

JSP - “<%...%>”VS“<%= ...%>”之间的区别是什么

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

While working with JSP files and servlets , I came across <% … %> and <%= … %> . 在使用JSP文件和servlet时,我遇到了<% … %><%= … %>

What's the difference between both cases ? 这两种情况有什么区别?

Thanks 谢谢

<%= … %> will echo out a variable, where as <% … %> denotes a script or some code that is executed. <%= … %>将回显变量,其中<% … %>表示脚本或执行的某些代码。

Here are the links to the jsp documentation: 以下是jsp文档的链接:

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

is same as 和...一样

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

There are three types of Scriptlets : Scriptlet有三种类型:

  • Scriptlet Expressions of the form <%= expression %> that are evaluated and inserted into the output Scriptlet表达式<%= expression%>的表达式 ,它们被计算并插入到输出中
  • Scriptlet of the form <% code %> that are inserted into the servlet's service method 插入到servlet的服务方法中的<%code%>形式的Scriptlet
  • Scriptlet Declarations of the form <%! Scriptlet声明形式<%! code %> that are inserted into the body of the servlet class, outside of any existing methods. 代码%> ,在任何现有方法之外插入到servlet类的主体中。 For ex: 例如:

     <%! public int sum(int a, int b) { return a + b; } %> 

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 ) %> . 它可以被视为<% out.print( something ) %>的快捷方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM