Java.Servlet.How to comment out code in JSP?

In JSP, there are three types of comments, depending on what exactly you want:


📝 1. HTML Comments

<!-- This is an HTML comment -->

It will appear in the browser’s page source (users can see it if they view source).

Used to explain client-side code (HTML, CSS).

🛡️ 2. JSP Comments

<%-- This is a JSP comment --%>
  • It is completely removed during JSP translation (server-side).
  • Browser will never see this comment — it stays only on the server.
  • Used to hide server-side Java code, instructions, or disable code temporarily.

Best way to comment out Java or dynamic code in JSP.

Example:

<%-- 
    <% String username = request.getParameter("username"); %> 
    <%= username %> 
--%>

Neither the Java code nor the output will be included in the HTML response.

⚙️ 3. Java Comments inside Scriptlets

If you’re inside a Java block (<% %>), you can use normal Java comments:

  • Single-line Java comment:
<%
    // This is a single-line Java comment
    int counter = 0;
%>

Multi-line Java comment:

<%
    /* This is
       a multi-line
       Java comment */
    int counter = 0;
%>

📜 Quick Table:

Comment TypeSyntaxVisible in Browser?Purpose
HTML Comment<!-- comment -->✅ YesClient-side notes
JSP Comment<%-- comment --%>❌ NoServer-side notes
Java Comment// or /* */ inside <% %>❌ NoComment inside Java code block

🎯 Summary

  • Use <!-- ... --> for HTML comments.
  • Use <%-- ... --%> for server-side JSP comments (best for hiding logic from browser).
  • Use // or /* */ when writing inside <% %> scriptlet blocks.
This entry was posted in Без рубрики. Bookmark the permalink.