JSP Expression with Example

Note: It is advised to read Scriptlet tutorial before proceeding this page.

The main building blocks of JSP code are Scriptlet, Expression and Declaration. In this tutorial let us see how to and where to use JSP Expression.

We know a JSP page contains both static code (written by Web Designer in simple HTML) and dynamic code (written by Web Programmer in JSP). The main code goes into the Scriptlet.

If the Programmer would like to place small piece of dynamic code (like a variable value, say name of the client) in running HTML, JSP Expression is preferable than Scriptlet. Infact, what expression can do, a Scriptlet also can do. But to insert small bit of dynamic code, Expression is easier. In the following program, both ways are shown. You accept expression is better at the end.

Example on JSP Expression

I write a HTML file with one text field only to concentrate on syntax part rather than code.

File Name: Employee.html

Learning JSP Expression

Enter Your Name

Observe, the action attribute calls Exp.jsp on the server. Now let us write the JSP.

To start with, I write small pieces code while being explained, then all I put in a file for execution.

<%
   String str = request.getParameter("t1");
%>

The above Scriptlet, retrieves the name of the client and assigns to a string variable str. I call this str as a dynamic code, in the sense, it changes with every client. request is one of the 9 predefined implicit objects of JSP. I would like to use str as follows. Say str is "S N Rao".

Hello Mr.S N Rao, best wishes of the day. Mr.S N Rao is friendly with everyone. I pray God to shower his blessings on S N Rao.

The above two lines is nothing but running HTML but with a small dynamic content of S N Rao placed here and there. How to insert str variable in place of S N Rao? Here I use two ways.

a) Using JSP Scriptlet

<% out.println(str); %> is known as Scriptlet. Replace S N Rao with this scriptlet.

Hello Mr.<% out.println(str); %>, best wishes of the day. Mr.<% out.println(str); %> is friendly with everyone. I pray God to shower his blessings on <% out.println(str); %>.

b) Using JSP Expression

<%= str %> is known as Expression. Replace S N Rao with this expression.

Hello Mr.<%= str %>, best wishes of the day. Mr.<%= str %> is friendly with everyone. I pray God to shower his blessings on <%= str %>.

Enough. You get the same output. Which you will prefer, <% out.println(str); %> OR <%= str %>? Which is easier to use? Definitely, it is Expression. Observe, the Expression does not carry even semicolon at the end. First of all, typing Expression is easier without the stuff of out and println() etc.; you accept. Just put an extra = symbol after <% to differentiate expression from Scriptlet for JSP container.

JSP Expression is more simpler in the following examples.

a) The value of 2 raised 4 times is <%= Math.round(Math.pow(2, 4)) %>
b) Pay an interest of Rs. <%= (5000*3*12))/100 %>

For small processing of variables in running HTML, as you observe, expression is easier to use.

You can write the above two statements in the following format also, but I do not prefer as in JSP, static content is separated from dynamic. This separation leads to more cleaner code to understand, develop and debug.

a) <%= "The value of 2 raised 4 times is " + Math.round(Math.pow(2, 4)) %>
b) <%= "Pay an interest of Rs. " + (5000*3*12))/100 %>

Now let us write the JSP file with all the above code fragments.

File Name: Exp.jsp

<%
       String str = request.getParameter("t1");
%>

Using Scriptlet 
Hello Mr.<% out.println(str); %>, best wishes of the day. Mr.<% out.println(str); %> is friendly with everyone. I pray God to shower his blessings on <% out.println(str); %>.
Using JSP Expression
Hello Mr.<%= str %>, best wishes of the day. Mr.<%= str %> is friendly with everyone. I pray God to shower his blessings on <%= str %>.
JSP Expression to use in other ways
a) The value of 2 raised 4 times is <%= Math.round(Math.pow(2, 4)) %>
b) Pay an interest of Rs. <%= (5000*3*12)/100 %>

Same output with other style of using expression
a) <%= "The value of 2 raised 4 times is " + Math.round(Math.pow(2, 4)) %>
b) <%= "Pay an interest of Rs. " + (5000*3*12)/100 %>

Output Screenshot of HTML when value is entered

strong

Screenshot of response of JSP

strong

Something more about JSP Expression
  1. The syntax of Expression is
    <%=
    some code
    %>
  2. Precaution is Expression does not carry semicolon a the end of the statement. Giving semicolon is an error.
  3. <%= some code %> separates the dynamic expression code from static HTML.
  4. The code of expression (like Scriptlet) goes into the service() method (actually it is, _jspService()) of generated servlet.
  5. At runtime, the whole expression tag is evaluated and converted into string and inserted into the static content.
  6. One JSP page contain any number of Expressions (like Scriptlets).
  7. The XML equivalent to expression is
    Syntax:
    <jsp:expression>
    expression
    </jsp:expression>

    Ex:
    <jsp:expression>
    "The value of 2 raised 4 times is " + Math.round(Math.pow(2, 4))
    </jsp:expression>

  8. The variable values of Scriptlet can be used in expression also. It is because the code of Scriptlet and expression goes into the same service() method of the generated Servlet.
    <%
       int principle = 5000;
       int time = 3;
       int interestRate = 12;
    %>
    
    The interest payable is Rs.<%= (principle*time*interestRate)/100 %> and pay before due date.
    

1 thought on “JSP Expression with Example”

Leave a Comment

Your email address will not be published.