JSP Scriptlet Tag

JSP scriptlet tag allows you to insert programming logic inside JSP page. Hence, you can put valid Java code within scriptlet tags. Each Java statement must be followed by a semicolon.

Syntax: - <% Java code %>

Whatever the code written inside scriptlet tag <% %> is compiled as Java code. This code can be accessible anywhere inside the JSP page. Any number of Java statements and local variables can be inserted in scriptlet tag. Although, scriptlet doesn't allow method declaration within it.

Example of JSP Scriptlet Tag

Here is a simple example of JSP scriptlet tag having a Java code within it.

index.jsp

<html>
<head>
<title>Tutorial and Example</title>
</head>
<body>
<h2><center>Welcome to Tutorial and example
<%
java.util.Date date = new java.util.Date();
out.print("<br>Last Accessed Time: "+date);
%>
</center></h2>
</body>
</html>
Output:

Internal Procedure

Internally, JSP container inserts the content of scriptlet tag into the jspService() method.

Internal code:

public void jspService(HttpServletRequest request, HttpServletrResponse response) throws ServletException, IOException
{
PrintWriter out=response.getWriter();
response.setContentType("text/html");
java.util.Date date = new java.util.Date();
out.print("<br>Last Accessed Time: "+date);
}