简体   繁体   English

JSP中的Java表达式和Java Scriplets之间的区别

[英]Difference between Java Expressions and Java Scriplets in JSP

I find myself needing to learn a little bit of JSP for my Software Engineering class. 我发现自己需要为我的软件工程课学习一点JSP。 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". 第一行代码片段应正确返回“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). 第二行代码也应正确返回“JSP is Fun”,因为它是一个表达式,这意味着它不需要使用分号来正常运行(并且不能使用分号)。

Am I missing something glaringly obvious, or is there really nothing more to this relatively simple question? 我错过了一些明显的东西,或者这个相对简单的问题真的没有了吗?

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. 第二个将打印JSP is fun因为它被<%= %>标记包围。

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: 另外,第一个代码片段也可以打印出JSP is fun的值,如果它是这样编写的话:

<% out.println(request.getParameter("myText")); %>

Expressions are used to print some value on the page, whereas scriptlets are statements. 表达式用于在页面上打印一些值,而scriptlet是语句。 Your best bet is to go and check the generated class. 你最好的选择是去检查生成的课程。

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

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