简体   繁体   中英

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. 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?

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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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