Tuesday, February 21, 2012

JSTL Set tag examples or <c:set> in JSP – Java J2EE Tutorial

JSTL set tag or <c:set> also called as JSTL Core tag library is a good replacement of <jsp:setProperty> jsp action which lacks lot of functionality and only allow you to set bean property. you can not set Map's key value or create a scoped variable by using <jsp:setProperty>. jstl <set> tag allows you to do all the stuff related to setting or creating variables or attributes. by using JSTL <c:set> tag you can :
1) Set bean properties
2) Set Map values
3) Create Scoped variable on page, request, session or application scope.

But <c:set> comes with its own caveat like <c:set> can remove attributes or variable if provided value is null or throw exceptions which we will here. we will also lots of examples of JSTL tag <c:set> to understand what Set can do and how to use JSTL <c:set> tag.

This article is in continuation of my earlier post on JSP servlet e.g.  Difference between URL Rewriting and URL Encoding in JSP Servlet? and  How to define default error page in J2EE web application  if haven't already read you may find some useful information.

JSTL Core <c:set> tag

JSTL Set tag example in JSPAs I said earlier JSTL Set tag or <c:set> is use to set bean properties, map value and to create scoped variables. <c:set> can be used in two different ways either by using "var" or by using "target". var is used to define name of variable needs to be created or whose values needs to be set. while "target" is used to denote Bean Object or Map whose values needs to be set. here is a simple example of JSTL <c:set> tag

<c:set var="currency" value="USD" />

JSTL Example : How to create variable in session scope using <c:set> tag in JSP

Above example of <c:set>  will create an attribute named "currency" with value "USD" in default scope which is page scope. you can also create attribute into another scope say session scope, in that case you need to specify scope with <c:set> attribute scope="". here is an example of creating variable on session scope using jstl set tag:

<c:set var="currency" value="USD" scope="session" />

value of variable can also be a runtime expression e.g jsp expression or EL expression. like the one which is shown in below example of set tag:

<c:set var="currency" value="${user.currency}" scope="session" />

here Container will copy bean property user.currency in "currency" variable.


JSTL Example : How to pass value in <c:set> tag body in JSP

Another variance of jstl <c:set> tag is that you can supply value in body instead of on attribute line. some time when value is long, giving it on body makes code more readable. here is example of supplying value on jstl set tag body:

<c:set var="currency" scope="session" >
        USD,EUR,AUD,INR
</c:set>


JSTL Example : How to remove attribute using <c:set> in JSP

Keep in mind that <c:set> can also remove a variable or attribute if value resolves to "null" during runtime. e.g. in below example

<c:set var="currency" value="${user.currency}" />

<c: set> tag of jstl will remove "currency" attribute from any scope if EL expression ${user.currency} will resolve to null.


JSTL Example : How to set bean property using JSTL <c:set> tag in JSP

All of above example were for setting attribute or creating variables, but <c:set> can also be used to set bean properties or map value. in that case instead of "var" we need to use "target" and "property" which will define bean and property name to be set. if "target" is map than "property" is name of key and "value" is value for that key. What is worth noting here is that <c:set target=""> must point to a real object and not the name of object as it was with <jsp:useBean> action. if "target" doesn't resolve into object than web container will throw exception. here is example of setting bean property using JSTL <c:set> tag in JSP:

<c:set target="currencyMap" property="USA" value="USD">

this is equivalent to currencyMap.put("USA", "USD"); just like before you can also supply value in body of jst <c:set> tag in jsp. here is another example of setting bean properties using JSTL <c:set> tag :

<c:set target="trade" property="currency" value="USD">

this example will set currency property of bean named "trade" and equivalent to :

trade.setCurrency("USD");

You just need to make sure that target should resolve into real object.


JSTL <c:set> - Points to remember

Here are few important points to remember on <c:set> tag from JSTL core tag library. I have seen we often overlooked these small subtle details which causes hard to find bugs in JSP:

1) <c:set> can remove attribute if value="null".
2) Value for JSTL <c:set> tag can be specified in body also.
3) JSTL set tag value can be runtime expression include JSP expression or EL expression.
4) In target version of <c:set> if target is pointing to bean and property is not valid property container will throw exception.
5) If variable pointed by "var" doesn't exists than JSTL <c:set> tag creates it for you only if value is not null.
6) Scope attribute is optional in JSTL <c:set> tag and default scope is page scope.


These were some examples of  JSTL core <c:set> or set tag. <c:set> is very convenient and provides lots of key functionality required in JSP pages. I recommend using <c:set> over <jsp:property> or JSP actions. Let me know if you come across any other good example of <c:set> tag which we can include here.

Other Java J2EE Tutorials you may like:

2 comments :

Anonymous said...

Great examples. I don't use JSTL set tag on daily basis which makes me to look for examples before doing anything with JSTL and your tutorial and examples helps a lot.

Sayan Guharoy said...

Here are few jstl example as well

Jstl examples

Post a Comment