Comments in JavaServer Pages - JSP

Often, you want to document your JSP, and you can use comments to do this.A comment can take several forms, and depending on which you use, the comment might or might not be seen by the client if they use the View Source command in their Web browser. You will see exactly how the different comments behave. As a Java programmer, you are no doubt familiar with at least two common forms for

inserting comments into source code:

Consider the JavaServer Page in Listing , along with its generated servlet in Listing .

Listing 2.1 Java Comments in JavaServer Pages

Listing 2.2 Generated Servlet for the JSP in Listing 2.1

Upon inspection of Listings and , you can see that the HTML comment from the first line of Listing is written out as static text by the _jspService method and, thus, appears in the content that is sent back to the client:

out.write(“<!--Here is an HTML comment–copied to the output--> ”);

The Java comments from the declaration block of Listing are copied into the generated servlet outside the _jspService method:

There should be no surprise here because you have already seen that the content of a declaration tag is copied in this way. Any Java comment, such as the following, that occurs inside a scriptlet

<%//A Java comment inside a scriptlet-copied into the generated servlet %>

is used to document source code inside the _jspService method (see Listing , line 17):

//A Java comment inside a scriptlet

The only comment that does not appear in the output seen by the client takes the form:

The HTML seen by a client for Listing is shown in Listing For example, the client could use the View Source command from a Web browser.

Listing 2.3 HTML for the JSP in Listing 2.1

With all these comments available, you are probably wondering which you should use! Because the JSP is the document that receives the most human attention, you normally use JSP comments within it. However, if you find yourself using large sections of Java code within the JSP, you might find it more readable to use Java comments within the scriptlets.

One thing that you normally do not want to do is place HTML comments within your JSP.The reason for this is that the HTML comments are embedded within the generated document and are not displayed to the user because most Web browsers ignore them.Thus, they just waste network bandwidth.The only time that you want to use an HTML comment within JSP is if you are certain that you want the comment to be visible to the users if they view the source of the document they are viewing


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JSP Topics