java <% ... %> 和 <%! ... %> 在 jsp 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3333138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:21:25  来源:igfitidea点击:

What is the difference between <% ... %> and <%! ... %> in jsp

javajsp

提问by Yanki Twizzy

I am adding some more functionality to a page that has the two tags mentioned in the title. I noticed that the variable I declare in <% ... %>cannot be used in <%! ... %>and vice versa. What is the difference between the two and how can I declare variables that could be used in the two tags

我正在向具有标题中提到的两个标签的页面添加更多功能。我注意到我声明的变量<% ... %>不能用于,<%! ... %>反之亦然。两者有什么区别,我如何声明可以在两个标签中使用的变量

回答by skaffman

<% ... %>is used to embed some java code within the main service()method of the JSP. It is executed during the rendering of the page.

<% ... %>用于service()在 JSP的 main方法中嵌入一​​些 java 代码。它在页面渲染期间执行。

<%! ... %>is used to define code outsideof the flow of the page, and therefore outside the main service()method. Typically, this was used to define utility methods that would be called from within a <% ... %>block.

<%! ... %>用于在页面流之外定义代码,因此在 mainservice()方法之外。通常,这用于定义将从<% ... %>块内调用的实用程序方法。

Both approaches are now obsolete, however. JSP EL, JSTL and tag classes are the preferred way of doing the same thing.

然而,这两种方法现在都已过时。JSP EL、JSTL 和标签类是做同样事情的首选方式。