Scripting Elements of JSP

JSP Scripting Element

JSP Scripting element are written inside <% %> tags. These code inside <% %> tags are processed by the JSP engine during translation of the JSP page. Any other text in the JSP page is considered as HTML code or plain text.

Example:
<html>
    <head>
        <title>My First JSP Page</title>
    </head>
    <%
       int count = 0;
    %>
    <body>
        Page Count is <% out.println(++count); %>
    </body>
</html>
 
 
Just to experiment, try removing the <% %> scriplet tag from the above code and run it as JSP. You will see that everything is printed as it is on the browser, because without the scriplet tag, everything is considered plain HTML code.

There are five different types of scripting elements

Scripting ElementExample
Comment<%-- comment --%>
Directive<%@ directive %>
Declaration<%! declarations %>
Scriptlet<% scriplets %>
Expression<%= expression %>

JSP Comment

JSP Comment is used when you are creating a JSP page and want to put in comments about what you are doing. JSP comments are only seen in the JSP page. These comments are not included in servlet source code during translation phase, nor they appear in the HTTP response. Syntax of JSP comment is as follows :

<%-- JSP  comment  --%>
Simple Example of JSP Comment

<html>
    <head>
        <title>My First JSP Page</title>
    </head>
    <%
        int count = 0;
    %>
    <body>
        <%-- Code to show page count  --%>
        Page Count is <% out.println(++count); %>
    </body>
</html>
 
 
NOTE : Adding comments in your code is considered to be a good practice in Programming world.

Comments