JSP vs HTML comments

Available

Another important point is comments in JSP. Always in the development process, there is a need to comment something out or leave memory for those brave guys who will support our code after it goes into production.

Commenting out any code inside the JSP is very simple, for this you need to use special "brackets":

<%-- a comment --%>

All code inside such brackets will be ignored when converting JSP to Servlet.

By the way, do not confuse this code with an HTML comment, which, as a reminder, looks like this:

<!-- HTML comment _ -->

Let's say you messed up and used an HTML comment in your code:

<html>
    <body>   <!--
    <%
        double num = Math.random();
        if (num > 0.95) {
            out.print(num);
        }
    %>  -->
    </body>
</html>

Here is the result:

public class HelloServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws Exception {
    PrintWriter out = resp.getWriter();
    out.print("<html> ");
    out.print("<body> <--");
        double num = Math.random();
        if (num > 0.95) {
             out.print(num);
        }
    out.print("-->");
    out.print("</body>");
    out.print("</html>");
    }
}

The HTML code will be commented out, but the Java code inside such comments will still be executed.

Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet