JSP Comments

JSP Comments: XML, JSP, and Java-Based Comments

In this article, I am going to discuss JSP Comments. Please read our previous article where discussed JSP Directives: Page, Include, and TagLib. At the end of this article, you will understand the following pointers in detail.

  1. Why we need Comments in JSP?
  2. XML Based Comments
  3. JSP Based Comments
  4. Java-Based Comments
Why we need Comments in JSP?

A JSP comment is useful when you want to hide any part of the JSP page. It marks text or statements that the JSP container should ignore. Use JSP comments whenever you want to comment out a part of your JSP page. Whenever you will use the JSP comment tag it will tell the JSP compiler to ignore the commented part from the compilation. So, the commented part will not be considered for the content parsed for the response. Whenever a comment is recognized by the compiler/interpreter then it places that portion code with white spaces.

Follow the below steps to add comments to a JSP page:
  1. Create a JSP page that begins with the scriptlet and can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language.
  2. On the page outside the scriptlet, keep any HTML tags.
  3. Now, add JSP comment using the <% – – – – %> tags.
  4. Also add HTML comments using <! – – – – > tags.

In JSP pages, we are able to use the following three types of comments:

  1. XML-Based Comments
  2. JSP-Based Comments
  3. Java-Based Comments inside Scripting Elements
XML-Based Comments

Also known as HTML Comments or Output Comments. It is used to comment on HTML code or template text of JSP programs. It will be recognized by an HTML interpreter. It is visible in JES source code, JES byte code as well as HTML that goes to the browser. It is not visible in the output. It is a comment that is sent to the client in the viewable page source. The JSP engine handles an output comment as uninterpreted HTML text, returning the comment within the HTML output sent to the client. You can see the comment by viewing the page source from your browser

Syntax: <! – – Comment – – >

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

Example
<html>
<head>
<title>A XML Based Comment Test</title>
</head>
<body>
 <h2>A Test of Comments</h2>
 <!-- This comment will not be visible in the page source -->
</body>
</html>

Output

JSP Comments: XML, JSP, and Java-Based Comments

JSP-Based Comments

Also known as Hidden Comments. It is used to comment on JSP tags. It will be recognized by the JSP page compiler. It is a comment that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment and doesn’t process any code within hidden comment tags. A hidden comment isn’t sent to the client, either within the displayed JSP page or the HTML page source. The hidden comment is beneficial once you want to cover or comment out a part of your JSP page. It is not visible in JES source, JES bytecode, HTML that goes to the browser as well as output.

Syntax: <% – – Comment – – %>

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

Example
<html>
<head>
<title>A JSP Based Comment Test</title>
</head>
<body>
 <h2>A Test of Comments</h2>
 <%-- This comment will not be visible in the page source --%>
</body>
</html>

Output

JSP Comments: XML, JSP, and Java-Based Comments

Java-Based Comments inside Scripting Elements

Java-based comments are used to comment on the java code of JSP programs. It will be recognized by javac. It is visible in the JES source. But is not visible in JES bytecode, HTML that goes to the browser as well as output. It is of the following types:

Single-line Comment

Just place “double backslash”(//) before the line to comment on a particular line.

Example: // Hello this line is commented.

Multi-line Comment

You can also comment on multiple lines in Java. Just place the lines between /* and */. It always starts with “/*” and ends with “*/”.

Example: /* Hello this is the way

To write multi-line comments in Java. */

Example (Single-line Comment)
public class Test {
 public static void main(String args[]) {
  // Declaring a variable named num
  int num = 1;
  // Printing the value of the variable num
  System.out.println("value of the variable num: " + num);
 }
}
Example (Multi-line Comment)
public class Test {
 /*
  * Following is the main method here, We create a variable named num. And, print
  * its value
  */
 public static void main(String args[]) {
  int num = 1;
  System.out.println("value of the variable num: " + num);
 }
}

In the next article, I am going to discuss JSP Scopes. Here, in this article, I try to explain JSP Comments. I hope you enjoy this JSP Comments article.

Leave a Reply

Your email address will not be published.