After learning Scriptlets, Expressions, Declarations and our own methods – all in one example, it is the time to learn about comments used in JSP.
We know JSP code contains static HTML code and dynamic JSP code. There are two types of of comments supporting static HTML and one type of comment supporting dynamic JSP code.
JSP 3 types of Comments
- Content comments (also popular as HTML comments)
- JSP comments (also known as Hidden comments)
- Scripting comments (also called as Java comments)
1. Content Comments
Content comments are nothing but HTML comments as they are used in HTML and XML part of JSP code. That is, content comments are written in static HTML code. These HTML comments are not recognized by Servlet (as internally a JSP file is converted to a Servlet) as they are not Java comments or defined in Java language. As they are not recognized by JSP (or to say, Servlet), they are sent to client as response along with other static content. This static content goes to client’s browser for displaying. But browser is aware of these content comments (because actually they are HTML comments only), they are not displayed to the user. If interested and if user is aware of, he can see them in View –> source menu of browser.
By chance, if dynamic content is placed in content comments, dynamic content is processed and sent to client, but seen in View –> source menu only.
Example, File Name: ContentComments.jsp
Output screenshot of above JSP file
The JSP contents, in the above example, contain all static HTML content only. Within the comment of <!– and –>, the math’s expression Math.pow(2, 63)-1 is placed. The math’s expression is processed and sent to client. See the output screenshot.
Another simple example:
<!– this program is written by S N Rao for his trainees –>
2. JSP Comments
Just now we understood that content comments are sent to client and can be viewed in browser’s View –> Source menu on client-side. Also we know, content comments are written in static HTML content. A similar comments also exist written in static HTML content only but are not processed and not sent to client at all. These are known as JSP comments. JSP contents does not appear even in (translated) Servlet source code.
Where exactly, JSP comments are used? Let us know, after seeing an example of how to use.
<% out.println("Hello 1"); %> <%-- <% out.println("This is not seen in the response."); %> --%> <% out.println("
Hello 2"); %>
Observe the screenshot, "This is not seen in the response." is not seen in the output.
While the JSP page is translated to a Servlet source file, any content placed within the delimters of <%- – and – -%> is simply ingnored by JSP container and not included in Servlet. For this reason, the content is not seen in the client’s response. Finally, to say, these comments are NOT seen by the user in the brower’s display or in View –> Source menu of client.
Some people call JSP comments as "Hidden comments" as they are hidden from client. It is preferred to use JSP comments instead of content comments.
JSP comments are used by Programmer in some following cases.
- To write some explanation for understanding difficult code for other programmers.
- To hide the comments from the user and also in Servlet source file.
- To document the JSP file like when the project started and Programmers involved in the project, copyright notice, description of JSP page etc.
JSP comments are seen only in the source code of JSP file.
Precaution: If you would like to use – -%> anywhere in the JSP body for other purpose than comment, use as – -%\>.
3. Scripting Comments
Scripting comments are placed in dynamic code like scriptlet or expression or declaration. As you can observe, infact they are nothing but Java comments. Why Java comments only? Because JSP dynamic code is written in Java, definitely the comments will be of Java only. First line is multiple line comments and the second line is single line comments.
Scripting comments will not appear in the source code of the generated Servlet. That is, these comments are not processed and not sent to client.
1. <% double p = 5000, t = 2.5, r = 12.5, interest = p * t * r /100; /* simple interest */ %> 2. <% double amount = p + interest // calculating the amount %>
Precaution: When single line comments are used, the end scriptlet tag %> must be placed in a separate line else this can also be read as part of comment and raises error.
File Name: ScriptingComments.jsp
Hello World <% /* The interest is calculated on at 10% rate */ %>
Output screenshot of above JSP file
Some people call Scripting comments as "Java comments".
4. How to write a comment in Scriptlet and Expression?
Following is Scriptlet comment
<% some code /* your another comment */ %>
Following is expression comment
<% /*= vector.size() */ %>
Note: Do not nest the comments. That is, do not write scripting comment in content comment etc., as it may lead compilation error.