logo

JSP Scriptlet Tag (Scripting Elements)


Show

In JSP, java code can also be written inside the jsp page by making use of the scriptlet tag. Firstly, let's check what are scripting elements.

JSP Scripting Elements

The scripting elements give the capability to insert java code inside the jsp. Scripting elements are of three types that are:

  • scriptlet tag
  • expression tag
  • declaration tag

JSP Scriptlet tag

For executing the Java source code in JSP a scriptlet tag is used. The syntax is given below:

<%  java source code %>  

Example of JSP Scriptlet tag

We are going to present a welcome message in the below given example:

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

JSP scriptlet tag Example that prints the user name

We have generated two files in this example that are index.html and welcome.jsp. The index.html file acquires the username from the user and the welcome.jsp file prints the username with the welcome message.

File:index.html

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

File: welcome.jsp

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