JSP - EL (Expression Language)

R Sudha

It is always a good practice to make use of a bean in our java programs. A JavaBean is nothing but a java class file which cannot run by itself. The bean may be either a valuebean or functionbean. A function bean can be used in other programs such as Console, Frame, TCP, RMI, Servlet even in  EJB and Web services etc. But when we use valuebean or functionbean   in JSP ,we have to be careful.(ie. The bean should always be placed in package)

For example, Let us say we have a ‘greeter’ bean as shown below

public  class greeter
public  String   greet() { return  "Hello!"; } }

Assume that we compile and place   it in ‘tomcat5\webapps\root\web-inf\classes’

let us write a servlet now

response.setContentType(“text/html”)   ;
PrintWriter out=response.getWriter();


greeter  greeter1=new greeter(); String s1=grreter1.greet(); out.println(s1);

This will work because the servlet file and bean class file are in same folder. Normally , When you don’t make use of a bean in servlet, the servlet code  can be placed within delimiters and simply saved as JSP!

<% String a = request.getParameter(“text1”); out.println  ( a); %>

This will work as JSP.

But, when we use a bean , the following code
will NOT  work!

  greeter  greeter1=new  greeter();
  String s1=grreter1.greet();



When you run the above jsp, It will show error. Because,  jsp  is  unable to find the bean. According to specification,  a bean which is to be used by JSP should be placed in package as  follows,

package mypack; 
public  class greeter {     public  String   greet()     {     return  "Hello!";     } }

Compile and place the class file in ‘tomcat5/webapps/root/web-inf/classes/mypack’ folder.

Now modify the jsp as shown below,


mypack.greeter greeter1=
  new mypack.greeter();
  String s1=grreter1.greet();


Now, this will work correctly. There is also one more solution .

<%@     page import=”mypack.*”      %>


greeter  greeter1=new  greeter();
  String s1=greeter1.greet();

So, this will work .We can make use of standard action tag <jsp:useBean…> also.

<jsp:useBean id=”bean1” 
  class=”mypack.greeter” />
String s1=bean1.greet(); out.println(s1);

So far we have made use of scriptlets. We can make use of Expressions also.

  <jsp:useBean id=”bean1”
  class=”mypack.greeter” />
  <%= bean1.greet()  %>

          This is actually a    tag.

Though the above methods for making  use of bean are very easy for  java programmers, page designers feel  the other way. They don’t want expressions even. They want standard tags and special tags (custom tags) and special scripting language.  That is what  is known as JSP  EL (JSP    Expression Language).

I am using tomcat5. If we go to web-inf/web.xml we will find dtd for servlet2.3 specification. JSP-EL will not work with this DTD. Therefore I created a separate context under ‘tomcat5/webapps’  named elapp. It will have usual directory structure of root directory. Change the DTD into 2.4 schema as follows

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd" version="2.4">

The best and easiest way for creating a new context is not  to start creating folders and typing.

Just bring the ROOT folder to desktop and rename as required (elapp) and paste it inside ‘tomcat5\webapps’. So the required directory structure is ready. After that, modify the 2.3  DTD  in web.xml into 2.4 Schema

Now we want to check whether el will work well. So, create a test.jsp in ‘tomcat5\webapps\elapp’ with following statement

${‘very good’}

And then, start your tomcat and goto ‘http://localhost:8080/elapp/test.jsp’. This will print

Very good

A Few words about JSP-EL

The JSP2.0 specification introduced EL which can do whatever a scriptlet can do. This language is much simpler to understand than java and looks very similar to javascript           

JavaScript is something that most page authors are already familiar with.          
The EL is inspired by ECMA script, which is  standard version of JavaScript.

Disabling Scriptlets:

The EL is intended to replace the use of Java scriptlets in developing JSP-based web applications. To this end it’s possible to prevent the use of scriptlets through configuration parameters. This allows  to ensure that no one  uses scriptlets instead of EL.

You can disable scriptlets within a page using web.xml deployment descriptor by choosing to disable evaluation of the following:

  • One page
  • A set of page
  • The entire application

The tags that you need to add to the development descriptor are within the<jsp-config> element. The following excerpt shows an example that disables scriptlets for all jsp pages within an application:


The <url-pattern> element can represent a single page, for example :



          <scripting-enabled>false</scripting-enabled>       </jsp-property-group>
It can also represent a set of pages, for example:
<url-pattern>/elapp</url-pattern>           <scripting-enabled>false</scripting-enabled>       </jsp-property-group> <jsp-config>

Disabling Expression Language:

Just as you can disable scriptlets within a page, you can also disable the evaluation of the EL. In previous versions of the  JSP, the character ${ had no special meaning,  therefore it is possible  that people have  used them in their JSP pages. If you were to try to deploy these pages on a jsp 2.0 compliant web container, you would get errors.

It  is worth noting that if a web application is deployed using a Servlet 2.3 deployment descriptor (that is, one that confirms to the 2.3 DTD) then the evaluation of the EL is automatically deactivated. . Conversely, if a wub application is deployed with a servlet 2.4 deployment descriptor (that is, it conforms to the 2.4 XML schema )then the EL is enabled by default.

As with the disabling of scriptlets, you can disable EL evaluation in two ways:

In order to temporally disable the EL  for a single page its simplest to use the is ELIgnored attribute of the page directive in the header of the page:

    <%@ page isELIgnored=”true” %>

If you choose to disable evaluation within the web.xml file, you can disable for a single page, a set of pages, or the entire application. The following XML example shows how you might disable the EL for an entire application:


In order to disable the evaluation of the EL  for a single page within the deployment descriptor, you can use sn XMl fragment similar to the following:


In order to disable the evaluation of the EL for a set of pages within the deployment descriptor, you can use an XML fragment similar to the following:



Let us now see, three simple examples .All  the jsp files  placed in ‘tomcat5/webapps/elapp’ folder whereas  class files if any, are placed in ‘tomcat5\webapps\elapp\web-inf\classes\mypack’ folder

( we are not using any bean here)


  <form       method=post    action="ex1.jsp">
  <input    type="text"       name="name">
<input    type="text"          name="place">
<input type="submit">


${  param  [“name”]   }
${  param  [“place”]   }

Example 2:
(  we have a bean here)

package mypack; public class person { String name=null; String place=null; public  void setName(String a){name=a;} public String getName(){return name;} public void setPlace(String b){place=b;} public String getPlace() {return place;} }



  <form      method=post    action="ex2.jsp">
  <input  type="text"       name="name">
<input  type="text"       name="place">
<input  type="submit">

//   ex2.jsp

<jsp:useBean id="person"  class="mypack.person" 

Example 3:

Nested Properties of  java bean:

Sometimes the value bean may have another  collection   as attribute.
In our case, it is address bean.

We use a servlet to collect the data from address.htm  page and populate the address bean and then it sends the object to the jsp.

For brevity, I am not showing the html form for collecting the address details.

The following code belongs to the servlet invoked by the html.

mypack.personbean person=
  new mypack.personbean();
mypack.addressbean address=
  new mypack.addressbean();
String s1=request.getParameter("name");
  String s2=request.getParameter("street");
  String s3=request.getParameter("town");
  String s4=request.getParameter("state");
  String s5=
  RequestDispatcher rd=request.getRequestDispatcher("/nested.jsp");



package mypack;
public class addressbean
  String street;
  String town;
  String state;
  String pincode;

  public void setStreet(String a) {street=a;} public String getStreet() {return street;}
public void setTown(String a) {town=a;} public String getTown() {return town;} //---------------------------------
public void setState(String a) {state=a;} public String getState() {return state;}
public void setPincode(String a) {pincode=a;} public String getPincode() {return pincode;}

} ---------------------------------------
package mypack; public class personbean {
String name; addressbean address;

public void setName(String a) {name=a;} public String getName() {return name;}
public void setAddress(addressbean a)
public addressbean getAddress() {return address;}

After compiling and deploying to classes\mypack folder, I write the JSP which uses these beans as follows.

Syntax alone being given.  ( carefully note the square bracket for accessing from the collection.)


  NAME:  ${person.name} <br>
  STREET:  ${person["address"].street}  <br>
  TOWN:  ${person["address"].town}    <br>
  STATE:  ${person["address"].state}   <br>
  PINCODE:  ${person["address"].pincode} 

EL is very important in modern JSP.

EL   is always meant for displaying the result and not for Business logic. How then are we to carry out the logic?
That is done by tags. (known as Custom tags ).

I hope that I have given a clear introduction. In the next instalment, I will deal with Functions in JSP-EL.

These topics are important for SCWCD Exam.