33 JSTL and EL Part 1

Pravin Jain

epgp books

 

Introduction 

 

Welcome to this module on JSTL and EL This is the first part of the module, we will have this JSTL and EL being covered in two modules. In the previous modules, we have been looking at JSP, The various kinds of elements going into a JSP. We started by covering the scripting elements, the basic scripting elements like scriptlets, expressions, declarations, directives. Then we have seen about the standard tags the action tags which can be included in a JSP, and then in the last few modules, we have seen about the use of custom tags.

 

We can build a tag library. We can create our own custom tags and then we can use them. We have this tag library, which you can create on you own and then include tags from there. Then there was some project from Apache about a set of tag libraries which could be of very common use. Later on this was called as Java Standard Tag Library. So, there is one JSTL, which is a project from Apache and in the next module we are mainly going to cover the most commonly used tags from the standard tag library.

 

Along with that, as you can see, there is another thing, not only tags, we have something more which can also be included in a JSP page. This is from JSP 2.0 onwards. From JSP 2.0 onwards, we can also include what we call as an EL, or an Expression Language. So another new element which can be used within a JSP, is the expression language.

 

What is EL 

 

Lets first look at the expression language and then we will move on the JSTL. The two are linked, because it is in this JSTL, we have got lots of tags in this library and most of these tags use dynamic attributes, and for most of these attributes expression language is a very handy tool which can be used. So lets try to understand what is the expression language about.

 

The syntax for including an EL expression in a JSP is ${ expression }. What is the expression in the EL which is between two braces. The syntax for the expression says, you can have an identifier over there. The identifier here is any Java identifier. In the expression language, to make things much simpler,  instead  of  starting  to  use  those  <jsp:useBean  ..>  and  then  having  to  write  those <jsp:getProperty …> tags in a JSP, we don’t need any of those things anymore. Using EL, from any bean available in any of the scope, any of its property can directly be used. For that what is the name of that attribute, that name of the attribute available in the scope should be a valid Java-identifier. It should follow the syntax of a Java-identifier, then it can be used in an EL straightaway.

 

identifier 

 

So, we have identifiers, not only that identifiers can be used in an expression of an EL. We can use any object available as an attribute in any of the scopes, we understand what are the four scopes. The page scope, the request scope, the session scope and the application scope. We can get hold of any attribute available in any of these scopes, by straightaway using them inside the EL, provided the attribute name is a valid Java identifier. So, this is something which we can use in our EL expression.

 

Another thing, which we can use in our EL is, we have some implicit objects which can be used inside any EL. We have a set of implicit objects, we will list those objects later on, but that is another thing which can used in the EL.

 

Operators 

 

So, the expression in an EL can be an identifier, along with this we also make use of operators in expression of the EL. We have standard operators available. Arithmetic operators, so, we use those +, -, *, /. because / has so many usages in HTML, so, another form this operator is available. Instead of using / we could just say div. This may be more convenient. This would not require any HTML escaping to be done. We also have the modulus operator, Here also either we use % or mod. So, this way we have the Arithmetic operators which can be use in the expression of an EL.

 

We also have the logical operators, which can be used in the expression of an EL. The logical operators available are && (double ampersand) for the and operator, || (double pipe) for the or, Here also we can use ‘and’ and ‘or’ as logical operators, instead of the symbols. We also have the unary logical operator !. The logical operator not, which can also be written using the letters ‘not’.

 

There is one more unary operator called empty. empty can be used for testing, if a particular variable is available, and if it is available, if it is of type String, then if it is a null or just an empty string. Those kinds of things can be tested with the help of empty operator.

 

Logical operators return value which is always going to be boolean type. We even have another category of operator, for comparision. So, we have these ‘<’ or ‘lt’, ‘>’ or ‘gt’, ‘==’ or ‘eq’, ‘!=’ or ‘ne’ these are the other kind of operators available here.

 

There are two very important operators, and very commonly used here in the expression language. One is the ‘.’ and the other is the ‘[]’. They are used for accessing the member of the object. We access the properties of the members by using the ‘.’ operator, or instead of ‘.’, you may even use the ‘[]’. ‘[]’ is more general, and has much more usage than just asking for a particular property of the object, or particular member of the object.

 

The ‘.’ operator 

 

Let us say, you have some kind of a Java Bean object, of a class Name, and Name class has separate instance variables for firstName and lastName, You would have those methods called getFirstName, getLastName to access them. So, we have properties, firstName and lastName, become the properties of Name. Now, let us say that you have some attribute in one of the scope for an object of type Person. So, we may have another class called Person and Person has one of the properties, as name, which is of type Name. So there is a getName method in class Person, and now, you have an attribute of type Person, and name of the attribute is john. So, I have got john as a attribute name, where it is an object of Person, and we understand that, once we have a Person, the Person has a name and name has firstName and lastName. So it should then be possible for you to use this ‘.’  operator.

 

How we use the ‘.’ operator in EL, we can say ${john.name.firstName} to get hold of the attribute object john and then the name because we john is of type Person and Person has a getName method. And then, we use the firstName, because Name class has a method called getFirstName.

 

So, this ‘.’ operator allows us to access the peoperty and then the sub-property if there is one available.

 

This something very commonly used.

 

When we design a JSP, and we are following the MVC pattern, We will be ahving so many objects available as attributes and the JSP’s job is to just render them. When it is trying to render them, we have the objects available and these objects would have their properties.

 

A JSP is just like a template, in which there are places where you will be interested in displaying certain values from the objects. Any of the properties of the object, which is to be displayed, or they have a sub—property, all that can very easily be done. We don’t now have to write too much of code, we don’t have to go for expressoins, or even writing scriptlet. All that needs to be avoided.

 

We can jsut make it very simple, using the EL. So, this way EL is very simple and ‘.’ is a very good operator, which is available for getting access to the properties and sub-properties of an object.

 

The ‘[]’ operator 

 

The other very important operator is the ‘[]’. ‘[]’ as mentioned earlier, it is much more general. ‘[]’ can also be used for accessing the property. We can use it for accessing the property. We should be knowing the property, if we know the property, So, if we consider the earlier example. We had used $}{john.name.firstName}. Now, instead of saying firstName, We have the other option, we can say, [“firstName”], to access the firstName subproperty.

 

So, one use of ‘[]’ is, pass a String to give the name of the property of an object. Another use of ‘[]’ is when we have an array type. We can always use this operator to access the element of an array by giving the index position. This is similar to the way we use it in Java code.

 

What about List, we understand, that List is an ordered collection, so, in our List object also, if we would like to use the ‘[]’ operator, we can use them to fetch the elements at the index position in a List. We need to pass the numeric value of the index position of the element to be fetched from the List.

 

Not only can the ‘[]’ operator be used for for accessing the elements from a List by index position, but it can also be used for fetching the value from a Map. As we know a Map is nothing but a key/value pair, a set of key/value pairs. So, if we know the key, we should be able to get the value object for it. Key can be any type. So here, if you have got a Map then the ‘[]’ operator can be used for getting the value corresponding to a key by passing the key object in ‘[]’. The type passed to the ‘[]’ operator must be the key type. So, if you give, this should be able to fetch the corresponding value for that key in the Map.

 

So, accessing members from Map is also something, which is very easily done by using the ‘[]’operator. So, ‘[]’ is much more general and versatile, but ‘.’ operator is also very convenient. So we have these two operators ‘.’ and ‘[]’. They can quite easily be used. These are the kind of things that can be used in our expression language.

 

literals 

 

So, for EL, we have so far seen that we can use identifiers and operators. We can also use literals. For expression language. We have all the literals, like we have literal null, we have the true and false for the boolean type, we can use literals of type String. We can use literals of type floating-point. Floating-point will be used as of type double and not of type float, and we can use int type. We cannot use the suffix ‘l’ or ‘L’ for the long type, we do not have the literal of long type available. So, this is another thing which we can use in the expression language.

 

Implicit objects 

 

Now let us talk about the various implicit objects, which are available to us for use in the expression language.

 

All the implicit objects, except for one of them, are of type Map. Let us have a look at each of them. We have a implicit object called cookie. cookie is a Map object available as implicit object. This Map, maps a cookie name with the corresponding Cookie object. It is not the cookie name and the cookie value mapping. It is a cookie name and the corresponding Cookie object. It is of type Map<String, Cookie>.  So,  if  we  want  to  get  the  value  of  the  cookie  with  a  particular  name,  we  can  use ${cookie[“name-of-cookie”].value}. Here the initial cookie[“name-of-cookie”] would fetch the object of type Cookie corresponding to the name of the cookie, and Cookie has the method getValue, to fetch the value from the Cookie object. Similarly we could also be using ${cookie[“name-of- cookie”].name} or any other such attribute of the Cookie, like expiration, version, etc.

 

Let us say we are interested in getting hold of the context parameters. For this we have another implicit object called initParam. This can be used for accessing the initialization paramteers for the application. It is a Map<String,String>. So in the initParam we can give the name of the init parameter and we will be able to get is the corresponding parameter value. So, we get fetch any iniitialization parameter, which has been declared as a context-param in the web.xml file.

 

When we have a request coming from a client, the request has got a lot of headers. We may be interested in accessing the headers, so we have a implicit object called header. When we use header, it is a Map, which maps the header name with the header value. So, it is a Map<String,String>. The key is the header name and value object is the header value. It may give you a null in case there is no such header.

 

In a request for the same header there can be multiple values. So, we have one more implicit object available as headerValues. The headerValues is also a Map, this maps a header name with multiple values. So it is a Map<String,String[]>. So, if we know there are multiple value for a particular header, we could always be using this particular implicit object.

 

Another thing which comes in a request are the parameters, there can be a number parameters ina request. So, we have this implicit object called param. The param object is nothing but a Map which maps the request parameter name with the parameter value. It is a Map<String,String>. So, if we want to fetch any parameter value, it is very simple ${param[“parameter-name”]}.

 

In an HTML form it is always possible to use same paramter name at multiple places. So, for a simgle parameter, there can be multiple values. So, we have this implicit object called paramValues, which maps the parameter name with multiple values. It is of type Map<String, String[]>.

 

The next implicit object is the pageContext. It is of type PageContext. It is our PageContext object, which we have seen in our earlier module. The pageContext in turn has getter methods for getting the request object, the response object, and other implicit objects available to a JSP. If we want ot access the request object, we could use ${pageContext.request}. We could fetch any attribute of HttpServletRequest   also,   eg.   If   we   want   contentLength   from   the   request,   we   could   use ${pageContext.request.contentLength}.

 

Another thing is about using objects from the four scopes. We understand that attributes from any of the scopes can be used directly. But that works only if the attribute name is a valid identifier. What if attribute name is not a valid identifier. If some attribute name has a space or a hyphen in it, we can’t use this in our EL directly. To fetch objects from any of the scopes, for all the four scopes we have a implicit object. We have the pageScope, requestScope, sessionScope and the applicationScope. These four implicit objects are of type Map. It maps the attribute name with the corresponding attribute object. So, they a Map<String,Object>. So, we may use ${requestScope[“attribute-name”]}. Here the attribute name need not be a valid identifier. So, this will allow us to use attributes whose name are not valid identifiers.

 

These are the implicit objects available for use in expression language.

 

This what we have in this module. So, this module has mainly covered, what is known as a expression language(EL). These are the things which will be very easily be used along with JSTL. The nexts module will cover about the most commonly use tags from the JSTL. There a lots of tags in the tag library, but we will covering the most commonly used tags. We most commonly follow the MVC pattern, and from that point of view, we will cover the tags from the JSTL.

you can view video on JSTL and EL Part 1

Suggested Reading:

  1. Core JSTL by David Geary, Pearson Education.
  2. Core Servlets and Java Server Pages Volume 2 by Marty Hall & Larry Brown, Second Edition, Pearson Education.
  3. Core JSP by Damon Haugland and Aaron Tavistock, Pearson Education
  4. Java Server Prog ramming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.
  5. http://docs.oracle.com/javaee/6/tutorial/doc/gjddd.html