JSP 中 Java 表达式和 Java Scriplet 的区别
我发现自己需要为我的软件工程课程学习一点 JSP。我们的家庭作业问题之一如下:
What are the output of these two code snippets if the parameter "myText" has the
value "JSP is fun"?
<% request.getParameter("myText"); %>
...and...
<%= request.getParameter("myText") %>
这是我的答案:
第一行代码片段应正确返回“JSP is Fun”。
第二行代码也应该正确返回“JSP is Fun”,如下所示 它是一个表达式,这意味着它不需要分号 才能正常工作(并且不能与一个一起工作)。
我是否遗漏了一些显而易见的东西,或者这个相对简单的问题真的没有什么其他的吗?
I find myself needing to learn a little bit of JSP for my Software Engineering class. One of our homework questions is as follows:
What are the output of these two code snippets if the parameter "myText" has the
value "JSP is fun"?
<% request.getParameter("myText"); %>
...and...
<%= request.getParameter("myText") %>
Here's my answer:
The first line of code snippet should properly return "JSP is Fun".
The second line of code should also properly return "JSP is Fun" as
it is an expression, which means it does not require a semi-colon
to function correctly (and would not work with one).
Am I missing something glaringly obvious, or is there really nothing more to this relatively simple question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个不会打印任何内容,因为它被
<% ... %> 标签包围。
第二个将打印
JSP is fun
因为它被<%= %> 标记包围。
标签中的
=
部分表示应该打印出标签内代码的返回值。附带说明一下,如果第一个代码片段是这样写的,那么它也可以打印出值
JSP is fun
:The first one will not print anything since it's surrounded with a
<% ... %>
tag.The second one will print
JSP is fun
since it's surrounded with a<%= %>
tag.The
=
part in the tag indicates that it should print out the return value of the code inside the tag.On a side note, the first code snippet can also print out the value
JSP is fun
if it was written as such:表达式用于在页面上打印某些值,而 scriptlet 是语句。你最好的选择是去检查生成的类。
Expressions are used to print some value on the page, whereas scriptlets are statements. Your best bet is to go and check the generated class.