JSP Comments

12 AM July 26, 2003

There are three main ways to put comments into JSP files: HTML comments, JSP “hidden comments” and Java comments.

HTML comments (reference)

<!- – This is an HTML comment – ->

The JSP engine will place HTML comments in the data stream sent to the user’s browser. This makes them useful for debugging, but they do take up bandwidth and are visible to users who select View/Source in their browser.

JSP hidden comments (reference)

<- – This is a JSP hidden comment – ->

Update: I had written the wrong syntax for JSP hidden comments. The above syntax is correct.

The JSP engine does not process the contents of hidden comments. They are useful for documenting as well as commenting-out sections of the JSP.

Java comments

<% /* This is a Java comment */ %>

Using both Java comments and JSP hidden comments requires some care. Java comments can be placed inside hidden comments, but hidden comments cannot be placed inside Java comments, because the ”>” part of the closing ”- ->” would terminate the Java block. 1

In my JSPs I default to using hidden comments, unless there is a good reason to use one of the other two. A simple hidden comment is almost always the best choice.

Sun’s Code Conventions for the JavaServer Pages Technology Version 1.x Language has more to say on this topic.


1 Your mileage may vary. While using WebSphere Application Server v4, I once mis-typed the end of a JSP hidden comment as ”- >” instead of ” -%>”. WebSphere placed the entire JSP starting from the end of the comment onto the outgoing data stream without processing it, and the browser ended up receiving the JSP tags. It took a good hour to figure out what was happening, and why.

By alang | # | Comments (4)
(Posted to Software Development)

Comments

At 05:41, 24 Sep 2003 Bruce Walker wrote:

JSP comments are of the form:
<%-- this is a comment --%>

The syntax given on this page, with an '!':
<%!-- this will not be interpreted as a comment --%>

will not work

(#)
At 09:31, 20 Oct 2003 Alan Green wrote:

Bruce: Aaargh. You are right. My apologies.

(#)
At 04:42, 04 Oct 2006 Jyothi wrote:

There are 2 comments in JSP
<!-- HTML Comments -->

<%-- JSP Comments --%>

and other HTML comment is

/* HTML Coding */

(#)
At 13:26, 07 Dec 2007 kiran mai wrote:

please suggest me the comments that are visible to the user

(#)

Add Comment




(Not displayed)






(Leave blank line between paragraphs. URLs converted to links. HTML stripped. Indented source code will be formatted with <pre> tags.)




© 2003-2006 Alan Green