This article contains basic Scriptlet Examples in JSP (Java Server Pages). Basically, we use a scriptlet to write Java code inside JSP. The following code syntax of scriptlet.

<% Java Statements %>

In fact, we can have various kinds of tags in a JSP including expression tag, declaration tag, and scriptlet tag. While we use a declaration tag to declare variables in Java. Further, an expression tag contains a Java expression that evaluates a value. Besides, a scriptlet tag contains java code like statements, loops, functions, and so on.

The following code shows a simple scriptlet that prints a text on the web page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center><h1>A Basic JSP Program</h1></center>
<% out.println("Hello, World!"); %>
</body>
</html>

Output

The output of Scriptlet Examples in JSP
The output of Scriptlet Examples in JSP