Wednesday, March 17, 2010

JSP Comments

While using JSPs, we should always try to use JSP-style comments unless you want the comments to appear in the HTML. JSP comments are converted by the JSP engine into java comments in the source code of the servlet that implements the JSP page. The jsp comment does not appear in the output produced by the jsp page when it runs. It is important to note that JSP comments do not increase the size of the file, but are useful to increase the readability of the JSP page.

There is only one type of JSP comment available by JSP specification.

JSP Syntax:
 <%-- comment --%>  

This JSP comment tag tells the JSP container to ignore the comment part from compilation. That is, it is not considered for the content parsed for 'response'.

Example:
 <%-- This JSP comment part will not be included in the response object --%>  

Important Points:
The following code is not a JSP comment.
 <!-- comment -->  
This is HTML comment. The JSP container treats this HTML comment tag as equal as any other HTML tags. When view source is done, the content given between this tag is visible. This is not anyway related to the JSP container, it is the expected behaviour of this HTML tag.

Example:
 <!-- This HTML comment part is included in the response object and can be seen in view source -->  

// or /* comment */ used inside the <% %> scriplet tag is also not a JSP comment. This is just a java comment and JSP container doesn't reserve any special treatment for this usage.

Therefore, there is no such thing as a hidden comment or output comment in JSP by specification.