Thursday, 9 May 2013

JSP Expression Language

JSP 2.0 EL

JSP’s Simplest Possible Expression Language (SPEL) is now integrated in 2.0 specification. Expression Language makes your life simple by getting rid of the need for knowledge in Java programming language and its more like ECMAScript. You can have your expression as follows.

Basic Arithmetic Operation

${1 + 2}

Basic Comaprison

${4.0 >= 3}

Usage of Implicit Objects

  • pageContext: The context for the JSP page.
  • param: Maps a request parameter name to a single value
  • paramValues: Maps a request parameter name to an array of values
  • header: Maps a request header name to a single value
  • headerValues: Maps a request header name to an array of values
  • cookie: Maps a cookie name to a single cookie
  • initParam: Maps a context initialization parameter name to a single value
  • pageScope: Maps page-scoped variable names to their values
  • requestScope: Maps request-scoped variable names to their values
  • sessionScope: Maps session-scoped variable names to their values
  • applicationScope: Maps application-scoped variable names to their values

Examples of using implicit objects

${not (pageContext.request.requestURI.endsWith('hello.jsp')}


EL also allows for function invocation. Functions are defined by tag libraries and are implemented by a Java programmer as static methods. You can also use default functions available under “”, but make sure, you have jstl.jar in your classpath.
First you need to create a tld file and refer in in your web.xml


 <taglib xmlns=””  
   <description>A sample function test</description>  
    <description>find the length of string</description>  
    <function-signature>java.lang.String findLength( java.lang.String )</function-signature>  
    <description>Remove last character</description>  
    <function-signature>java.lang.String remLast( java.lang.String )</function-signature>  

 public class Functions {  
 public static String remLast(String str) {  
 return str.substring(0, str.length()-1);  
 public static String findLength(String str) {  
 int len = str.length();  
 return “”+len;  
 public static void main(String [] args){  


 <%@ taglib prefix=”fn” uri=”” %>  
 <%@ taglib prefix=”wt” uri=”″%>  
 <form action=”testFunctions.jsp” method=”GET”>  
   <input type=”text” name=”elt”>  
   <input type=”submit”>  
 <div>Length : ${wt:findLength(fn:escapeXml(param["elt"]))} </div>  
 <div>After lstCharacter removed : ${wt:remLast(fn:escapeXml(param["elt"]))} </div>  
I am taking a break now. If I find any interesting features in JSP2, will be back creating a new blog under JSP 2.0 category.


  3. Examples given in apache tomcat server package

No comments:

Post a Comment