JSP - Scripting Elements

Java provides various scripting elements that allow you to insert Java code from your JSP code into the servlet. Scripting elements have different components that are allowed by JSP. Understanding each of these components is essential to writing code in JSP. Let us discuss each of the components in detail.

Scripting Element in JSP

Scripting elements in JSP must be written within the <% %> tags. The JSP engine will process any code you write within the pair of the <% and %> tags, and any other texts within the JSP page will be treated as HTML code or plain text while translating the JSP page.

Example:
<!DOCTYPE html>
<html>
    <head>
        <title>w3schools JSP Tutorial</title>
    </head>
    <% int cnt = 6; %>
    <body>
        Calculated page count is <% out.println(cnt); %>
    </body>
</html>
Here are five different scripting elements:
  • Comment <%-- Set of comment statements --%>
  • Directive <%@ directive %>
  • Declaration <%! declarations %>
  • Scriptlet <% scriplets %>
  • Expression <%= expression %>

Comment

Comments are marked as text or statements that are ignored by the JSP container. They are useful when you want to write some useful information or logic to remember in the future.

Example:
<%@ page import="java.util.Date"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Comments in JSP Page</title>
    </head>
    <body>
        <%-- A JSP comment --%>
        <!-- An HTML comment -->
        <!—The current date is <%= new Date() %>
        -->
    </body>
</html>

Directives

These tags are used to provide specific instructions to the web container when the page is translated. It has three subcategories:

  • Page: <%@ page ... %>
  • Include: <%@ include ... %>
  • Taglib: <%@ taglib ... %>

We will discuss these in detail in the coming chapter.

Declaration

As the name suggests, it is used to declare methods and variables you will use in your Java code within a JSP file. According to the rules of JSP, any variable must be declared before it can be used.

Syntax:
<%! declaration; [ declaration; ]+ ... %>
Example:
<!DOCTYPE html>
<html>
    <body>
        <%! int variable_value=62; %>
        <%= " Your integer data is :"+ variable_value %>
    </body>
</html>

JSP Scriptlet Tag

The scriptlet tag allows writing Java code statements within the JSP page. This tag is responsible for implementing the functionality of _jspService() by scripting the java code.

Syntax:
<% JAVA CODE %>
Example:
<!DOCTYPE html>
<html>
    <head>
        <title>Scriptlet Tag</title>
    </head>
    <% int count = 2; %>
    <body>
        Calculated page count <% out.println(cnt); %>
    </body>
</html>

Another code snippet that shows how to write and mix scriptlet tags with HTML:

Example:
<table border="1">
    <% for ( int g = 1; g <= cnt; g++ ) { %>
    <tr>
        <td>Number</td>
        <td><% = g+1 %></td>
    </tr>
    <% } %>
</table>

Expressions

Expressions elements are responsible for containing scripting language expression, which gets evaluated and converted to Strings by the JSP engine and is meant to the output stream of the response. Hence, you are not required to write out.print() for writing your data. This is mostly used for printing the values of variables or methods in the form of output.

Example:
<!DOCTYPE html>
<html>
    <body>
        <%= "A JSP based string" %>
    </body>
</html>

Scroll Back to Top