Yes, you can put comments in your JSP. If you’re a Java programmer with very little HTML experience, you might find yourself typing:
// this is a comment
without thinking twice. But if you do, then unless it’s within a scriptlet or declaration tag, you’ll end up DISPLAYING that to the client as part of the response. In other words, to the Container, those two slashes are just more template text, like “Hello” or “Email is:”.
You can put two different types of comments in a JSP:
<!-- HTML comment -->
The Container just passes this straight on to the client, where the browser interprets it as a comment.
<%-- JSP comment --%>
These are for the page developers, and just like Java comments in a Java source file, they’re stripped out of the translated page. If you’re typing a JSP and want to put in comments about what you’re doing, the way you’d use comments in a Java source file, use a JSP comment. If you want comments to stay as part of the HTML response to the client (although the browser will hide them from the client’s view), use an HTML comment.
Sharpen your pencil Answers
Valid and Invalid Expressions
Valid?
<%= 27 %> All primitive literals are fine. | |
<%= ((Math.random() + 5)*2); %> NO! The semicolon can’t be here. | |
<%= “27” %> String literal is fine. | |
<%= Math.random() %> Yes, the method returns a double. | |
<%= String s = “foo” %> NO! You can’t have a variable declaration here. | |
<%= new String[3] %> Yes, because the new String array is an object, and ANY object can be sent to a println() statement. | |
<% = 42*20 %> NO! The arithmetic is fine, but there’s a space between the % and the =. It can’t be <% =, it must be <%= . | |
<%= 5 > 3 %> Sure, this resolves to a boolean, so it prints ‘true’. | |
<%= false %> We already said primitive literals are fine. | |
<%= new Counter() %> No problem. This is just like the String[]... it prints the result of the object’s toString() method. |