Expression tag of JSP

In this post, you will learn:

  • What is expression tag in JSP?
  • What is the role of expression tag in the auto generated servlet?
  • How to assign the value of variables to the attribute of HTML elements?

The expression tag of JSP:

Expression tag in JSP has two use.

1. It is used to write the value of an expression to the output stream i.e. It provides a short hand mechanism of evaluating expressions and writing their values to the response.

2. It is used to assign the value of variables and expressions to the attributes of HTML elements.

It has following syntax:

<%= variable or expression %>

At the time of translation, for each expression tag of a JSP page, a statement is auto generated in the _jspService() method of the auto generated servlet to write the value of the expression to the output stream as demonstrated by the following diagram:

expression_tag_jsp

Description of the diagram:

1.0 Scriptlet statement is copied as it is to the _jspService() method of the auto generated servlet.

1.1 Statements are generated to write static HTML contents to the output stream in the _jspService() method of the auto generated servlet.

1.3 Statement is generated to write the expression value to the output stream in the _jspService() method of the auto generated servlet.

Practical example of the declaration tag:

In the following example, I have used the expression tag to present a filled form to the user as response.

First, the index.html :

<form method="post" action="adder.jsp">
First No. <input type="text" name="num1"><br/>
Second No. <input type="text" name="num2"><br/>
<input type="submit" value="add">
</form>

Second, the adder.jsp :


<!-- Scriptlet tag starts. -->
<%
/* Value of the request parameters is read. */
int a=Integer.parseInt(request.getParameter("num1"));
int b=Integer.parseInt(request.getParameter("num2"));
/* sum is calculated. */
int c=a+b;
%>
<!-- Scriptlet tag ends. -->

<!-- Using the expression tag, result is generated as a filled form -->
<form method="post" action="adder.jsp">
First No. <input type="text" name="num1" value="<%=a%>"><br/>
Second No. <input type="text" name="num2" value="<%=b%>"><br/>
Result <input type="text" name="result" value="<%=c%>" disabled><br/>
<input type="submit" value="add">
</form>

When a request is sent to the adder.jsp, the response is received in a form which has an additional text filed named result. It contains the sum of the given numbers and is disabled so that its value can’t be modified by the user. Using the response form, further requests can be sent to the adder.jsp.


Its time to recap the main concepts:

  1. Expression tag is used to write the value of a variable or expression to the output stream.
  2. For each expression tag of JSP, a statement is generated in the _jspService() method of the auto generated servlet.
  3. Expression tag can’t be used in the scriptlet or declaration tag.
  4. In a JSP page, any number of expression tag can be used.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *