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

 ${header["host"]}
${sessionScope.loggedInUser==null}
${not (pageContext.request.requestURI.endsWith('hello.jsp')}

Functions

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 “http://java.sun.com/jsp/jstl/functions”, 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
 <jsp-config>  
   <taglib>  
    <taglib-uri>http://chathurangam.net/blog-winster-jsp2</taglib-uri>  
    <taglib-location>/WEB-INF/my-taglib.tld</taglib-location>  
   </taglib>  
 </jsp-config>   

my-taglib.tld

 <taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd”  
   version=”2.0″>  
   <description>A sample function test</description>  
   <tlib-version>1.0</tlib-version>  
   <short-name>function</short-name>  
   <uri>http://chathurangam.net/blog-winster-jsp2</uri>  
   <function>  
    <description>find the length of string</description>  
    <name>findLength</name>  
    <function-class>personal.winster.technology.jsp2.el.Functions</function-class>  
    <function-signature>java.lang.String findLength( java.lang.String )</function-signature>  
   </function>  
   <function>  
    <description>Remove last character</description>  
    <name>remLast</name>  
    <function-class>personal.winster.technology.jsp2.el.Functions</function-class>  
    <function-signature>java.lang.String remLast( java.lang.String )</function-signature>  
   </function>  
 </taglib>  

Functions.java

 package personal.winster.technology.jsp2.el;  
 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){  
 System.out.println(remLast(“jsp2″));  
 System.out.println(findLength(“jsp2″));  
 }  
 }  

testFunctions.jsp

 <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>  
 <%@ taglib prefix=”wt” uri=”http://chathurangam.net/blog-winster-jsp2″%>  
 <form action=”testFunctions.jsp” method=”GET”>  
   <input type=”text” name=”elt”>  
   <input type=”submit”>  
 </form>  
 <br>  
 <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.

References

  1. http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html
  2. http://tomcat.apache.org/tomcat-5.5-doc/jasper-howto.html
  3. Examples given in apache tomcat server package

No comments:

Post a Comment