JSP Scriptlet tag

In this JSP tutorial, you will learn about two types of Directive tag namely Include and Tag Lib and also Scriptlet tag used in Java Server Pages along with syntax, usage, example and explanation for each of the tag.

Scripting elements

There are three types of scripting elements:

  • scriptlet tag
  • expression tag
  • declaration tag

scriptlet tag

It is the main scripting element of JSP. It is the main tag of JSP page that is used to define _jspService( ) method of Auto Generated Servlet i.e. contents of Scriptlets are placed in _jspService( ) method at the time of translation. All the statements which are placed within scriptlets are moved to _jspservice( ) method in the Auto Generated servlet at the time of translation

Syntax is as follows:

			 <%  java source code  %>
				 

Example of JSP scriptlet tag

In this example, we are displaying a welcome message.

 <html>
<body>
<% out.print("welcome to JavaTportal"); %>
</body>
</html>

Example of JSP scriptlet tag that prints the user name

we have created two files index.html and welcome.jsp. The index.html file gets the username from the user and the welcome.jsp file prints the username with the welcome message.

index.jsp

<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="Login"><br/>
</form>
</body>
</html>
			

welcome.jsp

 <html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>