JSP scriptlets example – JSP Scripting Elements with Example | Scriptlets in JSP | Definition, Syntax & Examples of Scripting Elements in JSP

JSP scriptlets example: In the previous tutorials, we have learned so many concepts related to JSP. In this tutorial, we will be going to learn about JSP Scripting Elements with Example. Primarily, there are 3 types of Scripting Elements in JSP. Get proper knowledge on JSP Comment, JSP Declaration, JSP Scriptlet, and JSP Expression from here and understand completely what is JSP Scripting Elements?

What are JSP Scripting Elements?

JSP comments syntax: JSP Scripting elements are utilized to enter java codes into JSP pages. To manages the objects, and do some computation on an object or runtime value, scripting elements are usually used. There are three scripting element types. These are the declaration, scriptlets, and expressions.

Syntax of Scripting Elements in JSP

The syntax starts with <% as follows:
<%! this is a declaration %>
<% this is a scriptlet %>
<%= this is an expression %>

Also Read: 

JSP Comment Tag

Comments are listed as text or statements that are overlooked by the JSP container. Moreover, JSP comment is utilized to note some parts of the JSP page to make it clearer and easier to manage. These comments in JSP are not involved in servlet source code while translation phase, nor they seem in the HTTP response.

The syntax of the JSP Comment is as follows:

<%-- JSP comment --%>

Example of Comment Tag in JSP

<%@ 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>

JSP Declarations Tag

Declaration tag in JSP Page is used to declare variables and methods. To define variables and methods of instance scope, it is used. Variable defined in declarations may be accessed from multiple threads, so they must be thread-safe. When JSP Page is initialized, then the declaration is initialized and is made available to other declarations, scriptlets, and expressions.

The syntax of Declaration Tag in JSP is as follows:

<%! declaration; [ declaration; ]+ ... %>

Example of JSP Declaration Tag:

JSP Scripting Elements with Example 1

JSP Scripting Elements with Example 2

JSP Scriptlet Tag

Scriptlet tag is used to write java code into JSP Page. It is typical that scriptlets are intermingled with template text. Because the variable and the code are local to the _jspService() method, therefore scriptlets don’t have the same thread-safety issue as declarations.

The syntax of Scriptlet Tag in JSP

<% java source code %>

Example of JSP Scriptlet Tag:

JSP Scripting Elements with Example 3

JSP Scripting Elements with Example 4

JSP Expression Tag

JSP Expression tag is used to print the value of variables and methods. At request processing time, the expression is evaluated, and then the result is converted into a String. If the result of the expression is an object, the toString() method is called. A ClassCastException will be raised if the value can’t be coerced into a String at translation time.

If you don’t know about Exceptions, we will be recommended you first learn the basics of Exceptions in Java.

<%=expression/statement %>

Example of JSP Expression Tag:

JSP Scripting Elements with Example 5

JSP Scripting Elements with Example 6