Are JSP expressions evaluated inside HTML comments of a JSP page?
Asked Answered
P

3

6

Are JSP expressions evaluated inside HTML comments of a JSP page?

i.e What would server output in this case?

<!--
Jeremy <%="Flowers"%>
--> 

Will the expression be resolved or will it remain as an expression in the HTML comment

a)

<!--

Jeremy <%="Flowers"%>

-->

or b)

<!--

Jeremy Flowers

-->
Pippin answered 20/2, 2011 at 21:42 Comment(1)
Instead of replacing < and > with html codes, you can select text and press ctrl+KPeshitta
C
5

Yes, the expressions will be resolved. The JSP page doesn't even know it is writing in HTML format, so it doesn't interpret anything HTML-specific.

You can also write plain text using JSP, or JSON, or whatever you like.

Crayfish answered 20/2, 2011 at 21:48 Comment(0)
P
2

Those are html comments, not jsp comments. So, all jsp code inside is still evaluated.

There's also jsp-specific way to comment content: <%-– ... -–%>. Content inside won't be evaluated by the server and won't be passed to the browser. So, it acts as html comment too.

Peshitta answered 20/2, 2011 at 21:44 Comment(4)
So the JSP parser (compiler) doesn't act smart in this respect? (I knew about the JSP comment for sure too :-) )Pippin
@Pippin You could say that. But I think it's not such a bad idea to separate new functionality (jsp) from existing one (html).Peshitta
This is really helpful to know and can be maddening if you don't. HTML comment tags do not prevent a JSP from reading JSP-related expressions, but JSP comment tags do. If you write <!-- ${el.expression} <%= JSP expression %> --!> the app server will still evaluate the JSP/JSTL/EL parts. If you write <%-- ${el.expression} <%= JSP expression %> --%> it won't.Selfcontained
While the rest of the answer is correct, "it acts as html comment too" is wrong. It doesn't act as an HTML comment, because as you said, it's not passed to the browser, and it won't be evaluated. HTML comments are evaluated by the JSP parser, and also passed to the browser (the browser then ignores the comments). But JSP comments are different that they are ignored by the JSP parser, so they don't "act" as HTML comments - they're one step ahead.Set
L
0

you can even use HTML comments to hide evaluated results from jsp expressions partially when the page is rendered to the browser.

e.g.

`${empty requestScope.errors? "" : "<p id='errors'>Error(s)!
    <ul>"}
    <!--${requestScope.errors.stream().map(x -> "-->
               <li>"x"</li>
           <!--").toList()}-->
    ${empty requestScope.errors? "" : "</ul></p>"}`

the local variable x from requestScope.errors.stream().map(x->) is still evaluated and passed to <li>"+=x+="</li>

output:

` Error(s)!

  • Product must have a name
  • Product must have a price
`

if the expressions are not in HTML comment tag, the "[]" symbol will appear due to .toList()

output without HTML tag:

`Error(s)!

    [
  • Product must have a name
  • Product must have a price
  • ]
`
Lift answered 9/4, 2021 at 2:7 Comment(1)
Please re-format your answer so that it could be read.Balfore

© 2022 - 2024 — McMap. All rights reserved.