Updated April 4, 2023
Introduction to JSP Scriptlet
The following article provides an outline for JSP Scriptlet. JSP (Java Server Pages) Scriptlet is a tag that is used to write java source code to implement business logic. This Scriptlet tag is declared inside the _jspService() method. _jspService()method always create new object for every new request. So as Scriptlet tag is inside the _jspService() method it will also create new object for every new request.
There are 3 types of tag in JSP.
- Declaration tag
- Expression tag
- Scriptlet tag
Each tag has their own specification to insert the code in the JSP application. Declaration tag is used to declare variables and methods. Expression tag is used to display output of the JSP application. Scriptlet tag is used to include Java source code. Scriptlet tag is mostly used for Form action pages implementation.
How to use Scriptlet Tag in JSP?
JSP Scriptlet used within <% %>(Scriptlet) tag. This will allow to include entire Java source code.
Syntax:
<% Java Source code %>
Like
<%
out.println("content");
<%>
Explanation:
- Scriptlet tag start with <% tag and ends with%> tag.
- Plain Java for printing the output we write System.out.println() but in case of JSP we just write out.println() and System class automatically provided by JSP web container.
- In JSP there are 9 implicit objects are there out is one among them.
Difference Between Declaration Tag and Scriptlet Tag
Given below is the difference between declaration tag and scriptlet tag:
Declaration Tag | Scriptlet Tag |
Declared inside <%! %> tag. | Declared inside <% %> tag. |
It can declare variables as well as methods. | It can declare only variables not methods. |
This tag is placed outside the _jspService() method. | This tag is placed inside the _jspService() method. |
It creates only one object for multiple requests. | It creates multiple objects for multiple requests. |
Examples of JSP Scriptlet
Given below are the examples mentioned:
Example #1
Display out.
JSP Code: Out.jsp
<%@ 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>
<!-- CSS Styles -->
<style type="text/css">
h1
{
color:green;
text-align: center;
}
p
{
color: blue;
font-size: 25px;
border: solid 3px brown;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Scriptlet Tag</title>
</head>
<body>
<h1>Introduction JSP Scriptlet Tag</h1>
<p>
<!--scriptlet tag -->
<%
//out is implicit object of JSP
out.print(
"JSP (Java Server Pages) Scriptlet is a tag used write java source code to implement business logic. This Scriptlet tag declared inside the _jspService() method. _jspService() method always create new object for every new request. So as Scriptlet tag is inside the _jspService() method it will also create new object for every new request. There 3 types of tag in JSP 1. Declaration tag 2. Expression tag 3. Scriptlet tag Each tag has their own specification to insert the code in the JSP application. Declaration tag is used to used to declare variables and methods. Expression tag is used to display output of the JSP application. Scriptlet tag is used for include Java source code.");
%>
</p>
</body>
</html>
Output:
Example #2
HTML to JSP action for display company name.
HTML Code: company.html
<html>
<head>
<!-- CSS Styles -->
<style type="text/css">
h1 {
color: green;
text-align: center;
}
input {
color: blue;
font-size: 22px;
height: 50px;
width: 300px;
}
button
{
color: white;
background: brown;
font-size: 25px;
}
label
{
font-size: 22px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Scriptlet Tag</title>
</head>
<body>
<form action="company.jsp">
<h1>Company Name</h1>
<!-- when you click on Submit button then page will gotocompany.jsp file and fetch the company details and display from there -->
<label>Which Company Are you working?:</label><input type="text"
name="company" placeholder="enter your company name"><input
type="submit" value="Submit"><br />
</form>
</body>
</html>
JSP Code: company.jsp
<%@ 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>
<!-- CSS Styles -->
<style type="text/css">
h1 {
color: orange;
text-align: center;
}
p {
color: navy;
font-size: 25px;
border: solid 3px brown;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Scriptlet Tag</title>
</head>
<body>
<h1>Well Come to My World</h1>
<p>
<!--scriptlet tag -->
<%
//out is implicit object of JSP
String name = request.getParameter("company");
out.print("I am working in " + name);
%>
</p>
</body>
</html>
Output:
Before Submit:
After Submit:
Explanation:
- Inside HTML page we enter company name and once we submit the details then action go to company.jsp file this company will display there.
Example #3
User Sign Up.
HTML Code: UserDetails.html
<html>
<title>Scriptlet Tag</title>
<head>
<style type="text/css">
h1 {
color: green;
text-align: center;
}
label {
font-size: 22px;
color: green;
}
</style>
</head>
<body>
<h1>Register User Details</h1>
<form action="User.jsp">
<table>
<tr>
<td><label for="register_name">Enter name:</label></td>
<td><input type="text" name="name" value="" id="register_name"
style="width: 160px" /></td>
</tr>
<tr>
<td><label for="register_password"">Enter password:</label></td>
<td><input type="password" name="password"
id="register_password" style="width: 160px" /></td>
</tr>
<tr>
<td><label for="register_email">Enter Email:</label></td>
<td><input type="email" name="email" value=""
id="register_email" style="width: 160px" /></td>
</tr>
<tr>
<td><label for="register_gender">Enter Gender:</label></td>
<td><input type="radio" name="gender" value="male" /><label
for="register_gendermale">male</label><input type="radio"
name="gender" id="register_genderfemale" value="female" /><label
for="register_genderfemale">female</label></td>
</tr>
<tr>
<td><label for="register_country">Select Country:</label></td>
<td><select name="country" style="width: 160px">
<option value="india">India</option>
<option value="pakistan">US</option>
<option value="africa">Brazil</option>
<option value="china">England</option>
<option value="other">Italy</option>
<option value="other">Others</option>
</select></td>
</tr>
<tr>
<td colspan="2"><div align="right">
<input type="submit" id="register_0" value="Sign Up" />
</div></td>
</tr>
</table>
</form>
</body>
</html>
JSP Code: User.jsp
<%@ 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>
<!-- CSS Styles -->
<style type="text/css">
h1 {
color: red;
text-align: center;
}
p {
color: green;
font-size: 25px;
border: solid 3px blue;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Scriptlet Tag</title>
</head>
<body>
<h1>Well Come to My Application Form</h1>
<p>
<!--scriptlet tag -->
<%
//getting all HTML parameters
String name = request.getParameter("name");
String email = request.getParameter("email");
String gender = request.getParameter("gender");
String country = request.getParameter("country");
//out is implicit object of JSP
out.print("My Name is " + name+"<br>");
out.print("My Email is " + email+"<br>");
out.print("My Gender is " + gender+"<br>");
out.print("My Country is " + country);
%>
</p>
</body>
</html>
Output:
Before Sign Up:
After Sign Up:
Conclusion
Scriptlet tag in JSP is used to include Java source code. It is always creating new object for each request. This purpose of this scriptlet tag is to develop form action applications.
Recommended Articles
This is a guide to JSP Scriptlet. Here we discuss the introduction, how to use scriptlet tag in JSP along with examples. You may also have a look at the following articles to learn more –