34 JSTL and EL Part 2

Pravin Jain

epgp books

 

Introduction 

 

Welcome to this module on JSTL and EL part two This is part two of our modules on JSTL and EL. In the last module, we have seen about expression language. It was mainly about the expression language. We have already explored, what all things go in a JSP.

 

We have already seen about the custom tags in a earlier module. The usage of tags, the tag library, how do we use a custom tag in our JSP. So, if I have to use tags from any tag library, how do we use it. Here, if want use tags from the JSTL. There are a few things which we should be having. We first need to download certain things. There two standard jar files, these are standard.jar and jstl.jar. These will be required to be kept ni a lib folder for the web application. There are those tld files,

 

The JSTL is mainly composed of four standard libraries. We have core tags, covers most of the commonly used tags, then we have format tags, which will cover about the formatting. Then we have the sql tags, which is related to database connection, getting things from a database, and we have the xml tags, which is about parsing XML.

 

What we will be looking at here in this module is most of the core tags, and some of the tags, which are the format tags, which are related to internationalization.

 

To use these JSTL tags, we will also need the tls files, not just those two jar files. We will need the four tld files, c.tld, fmt.tld, xml.tld and the sql.tld. These tld files can be kept in a folder within the web application folder. We may have a folder called tld under the WEB-INF and keep our tld files over there.

 

Using JSTL in a JSP 

 

How do we use the JSTL in our JSP. May be, we are core tags and format tags in our JSP, then in our JSP initially should be mentioning using the taglib directive like <%@ taglib uri=”/WEB- INF/tld/c.tld” prefix=”c” %> we normally use prefix ‘c’ for core tags. Similarly we will have another use of taglib for format tag as <%@ taglib uri=”/WEB-INF/tld/fmt.tld” prefix=”fmt” %>.

 

core tags 

 

Let us now start looking at the most common tags.

 

c:out 

 

One of the tags, which may not in much use now, is the tag called c:out.

 

The syntax would require value to be specified.

The attributes for this tag are value, for the value attribute, we can use the expression language, value is a compulsory attribute, we have certain optional attributes, we have default attribute, we could say default= and we give some default value, the object which we are trying to show, in value is empty or is not available, then ti can show the default value. Within the value if there some special characters like & or any such thing, do, we want to show & in the output, or the & has its own meaning in html, So, do we want to put the & in output, ie. We would like to have the escape sequence for showing the special character, So, for that we have another optional attribute, with Boolean value, called escapeXml. So, we can specify escapeXml=’true’ or escapeXml=’false’ accordingly escaping for the special characters will be done or not.

 

Another syntax for c:out is with the body containing the value for output. There is not value attribute here, the value is in the body, and the other two attributes default and escapeXml can be specified. We include the value for output within the <c:out …> and </c:out>

 

c:set 

 

Another tag, which if you are following MVC pattern, you may use it, but we may know about that. We have the tag c:set tag. You can use this to set the value of any of the beans, Any Java Beans property can be set. We can set the value in a Map, or set the value for any particular attribute, The syntax has c:set has attributes where we can specify value= the value to be set , var= the variable whose value is to be set, and we can specify the scope=, one of the four scopes in which this variable is to be set. The default scope will be page scope. If the object is already existing in the scope, on that object we can use c:set to set the value of the property, for that another attribute called property will have to be used. We can use value=’…..’ and property=’….’ The var, value and property attributes can also be used for setting the value in a Map. That is what c:set is.

 

c:remove 

 

Like c:set, we have another tag. We have c:remove, where we only specify the attributes var and scope. So particular attributes can be removed from the specified scope. Here we can specify from which scope that variable has to be removed.

 

c:catch 

 

In core tags, we have tags related to flow-control also. We have c:catch. It is a tag with a body. So, we can say c:catch, with an optional attribute var=. We can specify a variable name and then we can have body for the tag. The body will a jsp segment and then we have a </c:catch>

 

The idea of c:catch is, that within the body of the tag, if an exception occurs, it is not going to come out with exception, instead it will continue processing after the body of the c:catch. If the var has been specified than it would be a variable referring to the exception object, which getting thrown within this catch. So we have

 

<c:catch var=’some-var-name’> jsp segment

</c:catch>

 

within the body of the catch if any exception is thrown, that aobject is available in the variable.

 

c:if 

 

Continuing with other kinds of flow-control tags which are available. We have got c:if. c:if has an attribute called test. Here test would be a Boolean expression. We also have an attribute called value, we also an optional attribute called var. We can use c:if without any body and another syntax is with body. When we use c:if, the test will be evaluated, the value will be evaluated to be a Boolean. If the value evaluates to true, then then only the body of the tag will be evaluated for output, if the test happens to be false, it will not evaluate the body for output. If the var attribute has been specified then, the Boolean value which has been evaluated for the test attribute is stored in the variable specified by var. We can also specify the scope here, to the variable will be stored in the specified scope.

 

<c:if test=’expression’ var=’variable’ scope=’one of the four scopes’> some body content  </c:if>

 

c:choose, c:when, c:otherwise 

 

Another conditional tag has various parts, something like a switch-case. The c:choose has inner tags. Within c:choose and /c:choose, we can use c:when. So the body of c:choose, is broken into smaller parts, each part specified using c:when. For each of the c:when a test has to be specified.

 

When it comes to c:choose, it will evaluate the test in each of those c:when for whichever c:when the test evaluates to tru, the body of that c:when will be evaluated for output.

 

along with these c:when, at the end of all the c:when, you can also add a c:otherwise tag, it is an optional one. If we use c:otherwise, if the tests in all the c:when evaluates to false then he body of c:otherwise will be evaluated for output.

 

<c:choose>

<c:when test=’test condition 1’> body for first when

</c:when>

<c:when test=’test condition 2’>

body for second when

</c:when>

…..

<c:otherwise>

body for otherwise

</c:otherwise>

</c:choose>

 

So, c:choose is another conditional tag,

 

c:forEach and c:forTokens 

 

We also have tags for loops. We have got different kinds of data structures, like Map, Array, List and various kinds of things which are iterable. And we would like to evaluate each of those. You want to loop over these data structures.

 

For loops we have got two different tags. There is a tag called c:forEach, and another is c:forTokens. In the c:forEach, we have attributes called items, what we give as items is some object, which could be of type array, it could be of type List, it could be of type Set, it could be a Collection, or it could be a Map object, it could be an Enumeration, anything which is iterable. Then we have the attribute var=, the var here is the local variable which is within the c:forEach and /c:forEach, this variable will contain the current value from the items. So items is one attribute. In case we are choosing the Map, then the variable in the var attribute will contain the entry from the Map. The Map.Entry type, the key value pair would be available in the var. Another way of using forEach, would be where you may not use items, or inspite of items, we have these atttibutes called begin, end and step. These attributes begin end and step, will have integer values. The begin will specify that out of the items specified, from which position, should it start, it may be more applicable to an array or to a List, something which is ordered. So, we can give the beginning point, We can give the end index, ie. It should not go beyond this position specified using the end attribute, we can even give the step size, by default step will be one. 

 

Along with this, there is one more optional attribute called varStatus, varStatus is a variable which will contain an object, which has its own properties, properties like begin, end, to know that this is the begin row, or this is the end row. begin, end properties are Boolean type. It also has properties to know the current row number, or the index number. So, we have various properties in the varStatus. eg. If we have used <c:forEach varStatus=’status’ ….> then we can use status.begin, status.end as Boolean values, to know whether it is the beginning or the end row. Similarly status.number will tell us the current number as 1,2.3… . Or status.index to know the index as 0,1,2….. This one syntax, where we use items.

 

In the other syntax, We may not be using items. If we are not using items, in that case begin and end attributes would be compulsory, you can optionally give a step. Here also we still have the var and varStatus. The var would contain the integer value. So, when don’t specify items, then the begin and end give the range of integer values, which will be available in var..

 

So, forEach can be used for iteration over data structures.

 

c:forTokens 

 

The next core tag is c:forTokens. In the forTokens, we can specify items, The items here can be String, even in forEach, the items could have been a String, in that case the var for the forEach would contain the characters in the String iterating over the characters in the String. But in case of forTokens, the items would be a String, but this has an attribute called delims, we can specify delimiters. So, whatever is the String, we would use thiis delimited to break it into tokens. The variable in var would be used for making available the specific token. So, tokens are broken out from the tiems, by using the delimiters, and makes it available to you within the forTokens with the help of the variable in var. You can use this in case you have string wiht delimiters. Very commonly, we might have something, which is comma separated, when we have comma-separated values available as a single string, we can use this find each of the values by using the delimiters as comma, and iterate each of the values.

 

Core tags for urls 

 

There a few more core tags we have some tags like c:import.

 

All these tags have an attribute for url. If we are interested in redirecting, we can use c:redirect and give the url. If we are interested in importing content from another url into the current JSP, we also have the tag cal c:import. C:import can used for including the content from another url. We can specify url and we can also specify a variable using the var attribute. Here, if we specify a ver, that var would contain the content from the url. The entier content as a String can be made available to the variable. So, we use c:import to actually read the content from some url and make it available in a variable. Or you can even include it in your content. Here we can use any URL, The URL here need not be within you application. It can go outside the web application. This much more powerful than something like RequestDispatcher.include. Because request dispatcher includes, content from only within the web application.

 

We, have another useful tag here. When session tracking is to be done, we understand, that session tracking is normally done using cookies, but instead of cookies, some servers may not use cookies, and may like to follow url rewriting. Now, if there is a url rewriting. It will require a lot of effort to rewrite each and every place where we use a url, so, wherever you have a url, which is within your application,. We would like to have sessionid being included and do a url rewriting. If we have to do url rewriting, how do we generate the url to include the session id. For this we the tag called c:url, here we just specify the local url in the value attribute. This local url would automatically append the sessionid. Thus url rewriting can be done using thies tag.

 

So, these are the commonly used core tags.

 

Format tags 

 

Other than the core tags, we have the next category of tags called the format tags. In this format tags here, we are going to be looking only at the internationalization related tags.We will not cover all the format tags here.

 

Recollect, in internationalization, we have the Locale class.

 

fmt:setLocale 

 

We have a tag called fmt:setLocale, this locale has attribute value = . the value here has to be object of a Locale, you specify a string also. eg. “en_US”. Here it also allowed to use a hyphen instead of underscore. eg. “en-US”. We also have an optional attribute called variant. This can used for specifying a variant. So, using thei tag, we can specify the setting for out Locale. If we want to use the Locale preferred by the user available from the request, we could specify that as <fmt:setLocale value=”${pageContext.request.locale}”. This would set the locale for the current page to the clients preference for the locale. So, in a JSP we would first use fmt:setLocale and then use the other tags for internationalization.

 

fmt:setBundle 

 

fmt:setBundle is used for specifying the current resource bundle. We have the attribute basename to specify the base name of the resource bundle. This will fetch the resource bundle according to the locale setting as specified by using a fmt:setLocale tag.

 

fmt:message 

 

We have the tag called fmt:Message. When we use fmt:message, we specify property=, we give name of the propety or name of one of the resources available from the resource bundle. Using this we should be able to fetch any of the properties available in the current resource bundle specified using fmt:setBundle.

 

fmt:bundle

 

Some places we may have requirement of using resources from another resource bundle which is not current. The fmt:bundle will be used with a body, within the body of the fmt:bundle, we may use fmt:message, in this case it will use the resource from the one specified with fmt:bundle and the current resource bundle specified using fmt:setBundle.

 

fmt:param

 

The fmt:message can use a inner tag called param it has attribute name = and value = . This can be useful if our message has paramters similar to the parameters available in the MessageFormat class of the java.text package.

 

This is what have from the format tags related to internationalization.

 

Summary of format tags:

 

So we have seen fmt:setLocale for setting the current locale. Fmt:setBundle to set the current resource bundle and for smaller segments we can use fmt:bundle. When we use fmt:message, it would normally use for the current resource bundle as specified by fmt:setBundle or if it is insidwe fmt:bundle then from the one sprcified by fmt:bundle. This way we can include things which are from a resource bundle in our JSP. So, if we have done localization and created resource bundles, we should be able to use thm in our JSP.

you can view video on JSTL and EL Part 2

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 Programming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.
  5. http://www.codejava.net/java-ee/jstl/introduction-to-jstl