How to use comments in JSP pages?
There are two types of comments in a JSP page: server side comments and client side comments.
- The
server side comments, orhidden comments, is visible only on the server side because it is removed during JSP page translation.A
server side commentsis of the form<%-- comments --%>
This type of comments can be used for documentation, such as the author(s), the date, and the copyright notice of the revision, an identifier and a description about the JSP page for web developers. All these information will be ignored by the JSP engine at compile time and will not be propagated to the client side.
This type of comments can also be used to "comment out" some portions of the Java code of a JSP page.
An alternative way to place a "comment" in JSP is to use the comment mechanism of the scripting language. For example:
<% /** this is a comment ... **/ %>
However, pure JSP comments are preferred over JSP comments with scripting language comments, as the former is less dependent on the underlying scripting language, and will be easier to evolve into JSP 2.0-style pages. The following table is quoted from Code Conventions for the JavaServer Pages Technology Version 1.x Language:Line JSP scriptlet with scripting language comment Pure JSP comment single <% /** ... */ %>
<% /* ... */ %>
<% // ... %><%-- ... --%>
multiple <%
/*
*
...
*
*/
%>
<%--
-
...
-
-- %>
<%
//
//
...
//
%>
- The
client-side style, oroutput comments, that are intended to appear in the generated document sent to the client. They should not contain information about the behavior and internal structure of the server application or the code to generate the responses.In order to generate comments that appear in the response output stream to the requesting client, the HTML and XML comment syntax is used, as follows:
<!-- comments ... -->
These comments are treated as uninterpreted template text by the JSP container. If the generated comment is to have dynamic data, this can be obtained through an expression syntax, as in:
<!-- comments <%= expression %> more comments ... -->
The use of client-side comments is generally discouraged, as a client / user does not need or read these kinds of comments directly in order to interpret the received responses. The client-side comments for HTML authors to use a small amount of HTML comments to embody the guidelines of the HTML document structures, to break down the HTML code into sections for easy reading when viewing the page source. Sometimes it can be also used for highlighting copyright information. As for the preferred style of the client-side comments, the following is also quoted from Code Conventions for the JavaServer Pages Technology Version 1.x Language:Multiline Comment Block
A multiline comment block, be it JSP or client-side, is decorated with the dash character "-". In the XML specification, the double-dash string "--" is not allowed within an XML comment block. Thus, for compatibility and consistency with this specification, no double-dash string is used to decorate comment lines within a multiline comment block. The following table illustrates this preference using a client-side comment block:
Preferred Non-XML compliant <!--
- line 1
- line 2
...
-->
<!--
-- line 1
-- line 2
...
-->
Note either type of comments can be nested, so do not put ending quotes inside the comment itself.
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- What is difference between page and pageContext in JSP pages?(10521)
- How to config a JSP file in web.xml?(8592)
- How to disable browser caching for a specific JSP?(7121)
- How to retrieve values from HTML form input elements in JSP?(6900)
- What is the difference between jsp:forward and response.sendRedirect?(5432)
- How to get the current JSP page name?(5407)
- What is the difference between request.getParameter() and request.getAttribute()?(4989)