Home Jsp Simple-jsp-example Expression Language Basic Arithmetic

Related Tutorials


 
 

Share on Google+Share on Google+

Expression Language Basic Arithmetic

Advertisement
EL means the expression language , it makes it possible to easily access application data stored in JavaBeans components.

Expression Language Basic Arithmetic

        

EL means the expression language , it makes it possible to easily access application data stored in JavaBeans components. The jsp expression language allows a page author to access a bean using simple syntax such as $(name).  Before JSP 2.0, we could  use only  a scriptlet, JSP expression, or a custom tag to include server state in the jsp page output. Expression Language (EL) was first introduced in JSTL 1.0. EL makes it easier to integrate server side state with the presentation output. 

EL expression is always written between the delimiters ${ and }. In the JSP page response, the result of expression evaluation replaces the expression and its delimiters in the template text. Within tags, expression can be used only in attribute values.

The basic Expression Language arithmetic are: Addition (+), subtraction (-), multiplication (*), division (/ or div), and modulus (% or mod) are all supported in Expression Language. Error conditions, like division by zero are handled easily by the expression language.

In this example we are just using the arithmetic expression for EL.

The code of the program is given below:

<html>
  <head>
    <title>Basic Arithmetic</title>
  </head>
  <body>
    <h1>Basic Arithmetic</h1>
    <hr>
    In this example we are doing basic arthmetic.
 Error conditions, like
    division by zero, are handled gracefully.
    <br>
    <blockquote>
      <code>
        <table border="1">
          <thead>
	    <td><b>EL Expression</b></td>
	    <td><b>Result</b></td>
	  </thead>
	  <tr>
	    <td>\${2}</td>
	    <td>${2}</td>
	  </tr>
	  <tr>
	    <td>\${2 + 2}</td>
	    <td>${2 + 2}</td>
	  </tr>
	  <tr>
	    <td>\${2.2 + 2.3}</td>
	    <td>${2.2 + 2.3}</td>
	  </tr>
	  <tr>
	    <td>\${2.2E4 + 2.4}</td>
	    <td>${2.2E4 + 2.4}</td>
	  </tr>
	  <tr>
	    <td>\${-2 - 2}</td>
	    <td>${-2 - 2}</td>
	  </tr>
	  <tr>
	    <td>\${2 * 2}</td>
	    <td>${2 * 2}</td>
	  </tr>
	  <tr>
	    <td>\${2/4}</td>
	    <td>${2/4}</td>
	  </tr>
	  <tr>
	    <td>\${2 div 4}</td>
	    <td>${2 div 4}</td>
	  </tr>
	  <tr>
	    <td>\${2/0}</td>
	    <td>${2/0}</td>
	  </tr>
	  <tr>
	    <td>\${12%4}</td>
	    <td>${12%4}</td>
	  </tr>
	  <tr>
	    <td>\${12 mod 4}</td>
	    <td>${12 mod 4}</td>
	  </tr>
    <tr>
      <td>\${(1==2) ? 3 : 4}</td>
      <td>${(1==2) ? 3 : 4}</td>
    </tr>
	</table>
      </code>
    </blockquote>
  </body>
</html>

The output of the program is given below:

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 9, 2008

Related Tutorials

Discuss: Expression Language Basic Arithmetic  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Harold
May 26, 2011
JSP- Expression language(JSTL)

Why do we realy need JSTL than Scriptlet, i find it hard to use Expression language than the scriptlet
DMCA.com