JSP and JSTL experiences

Share this on .. Tweet about this on TwitterShare on LinkedIn0Share on Facebook0Share on Google+0Email this to someoneShare on Tumblr0Buffer this page

This post describes some of my recent experiences in using JSP and JSTL. For many probably highly trivial.

A dynamic include of a JSP (fragment), using jsp:include, allows passing of parameters, using jsp:param. However, such parameters are always String values. I needed to pass an array (String[]) and could not figure out how to. Using a scriptlet to define an array, add it to the pageContext (see for example) and reference it in the JSP fragment did not work:

  String[] lov = {"a","b", "c"};
  pageContext.setAttribute("lov", lov);

This is only logical once you realize that a jsp:include turns over control to a JSP that to all intents a purposes is a new, stand-alone JSP with its own pageContext. However, JSTL comes to the rescue. It turns out that the forEach tag recognizes a comma separated list of values. Note however that JSTL does not now consider the parameter a String[], it is still a String.

Code to call the JSP fragment:

<jsp:include page="lovPopup.jsp" flush="true">
                    <jsp:param name="lovParam" value="a,b,c"/>

Code in called JSP fragment:

<bean:parameter id="lov" name="lovParam"/>
  <c:forEach var="listItem" items="${lov}"/>
     <c: out value="${listItem}" />

It is important that the JSP fragment is also “JSTL enabled” (initially I erroneously thought that somehow this JSP would inherit the taglibraries imported by the JSP that invokes it), by importing the Core JSTL library:

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

Now it is not possible to refer to

${lov [1]};

even though forEach knows how to iterate through a comma-separated String, it does not mean that it is equivalent to a String[] (error: Unable to find a value for “1” in object of class “java.lang.String” using operator “[]” (null)).

It should be possible to put an attribute of type String[] in the requestScope, rather than the pageScope, and thus transfer a typed object, rather than just a String, to the included JSP fragent.

  String[] lov = {"a","b", "c"};
  pageContext.setAttribute("lov", lov, PageContext.REQUEST_SCOPE);

and to get access to the lov object in the included JSP fragment:

  <c:set var="list" value="${lov}" scope="request" />
  <c: out value="${list [1]}" />
  <c:forEach var="listItem" items="${list}"/>
  <c: out value="${listItem}" />
Share this on .. Tweet about this on TwitterShare on LinkedIn0Share on Facebook0Share on Google+0Email this to someoneShare on Tumblr0Buffer this page

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PL/SQL), Service Oriented Architecture, BPM, ADF, JavaScript, Java in various shapes and forms and many other things. Author of the Oracle Press books: Oracle SOA Suite 11g Handbook and Oracle SOA Suite 12c Handbook. Frequent presenter on conferences such as JavaOne and Oracle OpenWorld. Presenter for Oracle University Celebrity specials.

1 Comment

  1. how to use the jstl core(url) for querystrings.i want to pass the url with query strings.
    even i can not able to use this query string url for ‘set’ tag all so .plz give me possible solution.