Updated April 6, 2023
Introduction to Expression Tag in JSP
JSP (Java Servlet Pages) expression is tag used to display output streams. Expression tag subpart of the “JSP Scripting elements”. Display the output to the end-user we no need to writeout.println() method because this expression tag is working similarly out.println() method. The expression tag is represented as <%= %>. This expression tag backend code is out.println() only. This expression tag converts everything into a String object.
How Does JSP Declaration Tag Work in JSP?
1. The expression is worked based on the value provided within <!= %>( expression tag) tag. Basically inside this tag we write variables output or method output or plain text output.
Syntax:
<%! variables or methods declaration%>
2. Difference between Expression tag and Scriptlet tag: For better understanding of JSP declaration tag you must understand the difference between expression tag and scriptlet tag.
Expression Tag | Scriptlet Tag |
Declared inside <%= %> tag. | Declared inside <% %> tag. |
It can print the output. | It can declare only variables. |
This tag back up by out.println() method. | This tag back up by the _jspService() method. |
Examples to Implement of Expression Tag in JSP
Below are the examples:
Example #1 – Displaying Date and Time
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Expression Tag</title>
<style type="text/css">
h1
{
color: green;
text-align: center;
}
p
{
color: blue;
font-size: 22px;
border: 2px solid brown;
}
</style>
</head>
<body>
<h1>Displaying current date and Time by using Expression Tag</h1>
<p>
Current Date and Time:
<%= java.util.Calendar.getInstance().getTime() %>
</p>
</body>
</html>
Output:
Example #2 – User Registration with Expression Tag
Code:
<!DOCTYPE>
<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="Register.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: Register.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 Expression Tag</title>
</head>
<body>
<h1>Well Come to My Application Form</h1>
<p>
<!-- expression tag -->
<%="My Name is " + request.getParameter("name")%>
<br>
<%="My Email is " + request.getParameter("email")%>
<br>
<%="My Gender is " + request.getParameter("gender")%>
<br>
<%="My Country is " + request.getParameter("country")%>
<br>
</p>
</body>
</html>
Output:
Example #3 – Plain Text with Expression Tag
Code:
<%@ 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: navy;
text-align: center;
}
h2 {
color: blue;
}
p {
color: fuchsia;
font-size: 25px;
border: solid 3px red;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Expression Tag</title>
</head>
<body>
<h1>Plain Text with Expression Tag</h1>
<h2>First Paragraph</h2>
<p>
<!-- expression tag -->
<%="JSP (Java Servlet Pages) expression is tag used to display output streams. Expression tag sub part of the JSP Scripting elements. Display the output to the end user we no need to write out.println() method because this expression tag is working similarly out.println() method. This expression tag backend code is out.println() only. The expression tag converts everything into String object."%>
</p>
<br>
<h2>Second Pargraph</h2>
<p>
<%="JSP (Java Servlet Pages) expression is tag used to display output streams. Expression tag sub part of the JSP Scripting elements. Display the output to the end user we no need to write out.println() method because this expression tag is working similarly out.println() method. This expression tag backend code is out.println() only. The expression tag converts everything into String object."%>
</p>
</body>
</html>
Output:
Conclusion
The expression tag in JSP is used to display the output of the variables or methods or plain text. Data within the expression tag always treated as string type only. This expression tag is a short form of out.println() method.
Recommended Article
This is a guide to JSP Expression. Here we discuss the Introduction and how does JSP Declaration Tag Work in JSP along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –