How do you create an array using JSTL / EL?

Is it possible to create an array using EL in JSTL tags? For example, I would like to create something like this, only using JSTL and EL:

String[] myArray ={"one","two","three"};

I have read through the specification for both 1.0 and 1.1, and I could not find anything that mentioned it. Right now, I have to be able to do something using 1.0, but if this is only possible using 1.1, then I am still interested in how you do it. We should be upgrading our container to one that can use 1.1 sometime later this year.

I have tried the following, without success:

<c:set var="myArray" value="'one', 'two', 'three'" />

<c:set var="myArray" value="['one', 'two', 'three']" />

<c:set var="myArray" value="{'one', 'two', 'three'}" />

If anyone knows if this can be done and how to do it, I would greatly appreciate your ideas.

Thanks in advance.

[1209 byte] By [wood_taha] at [2007-10-3 0:05:42]
# 1

Nope, can't be done.

EL is not a programming language in its own right. It is not there to replace java code 100% - just to remove scriptlets from JSP pages.

Often if you can't do something in JSP, you have to ask yourself - should I be doing this in JSP, or does it belong in a bean/servlet?

In this case a simple bean to declare the options as an attribute will do it easily. EL can then access that bean.

evnafetsa at 2007-7-14 16:53:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks for the info. I didn't think it could be done, but if there was some trick out there, I wanted to know about it.
wood_taha at 2007-7-14 16:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Is the following right? There is an asymmetry with EL/JSTL between reading and writing arrays: you can read arrays, but cannot edit them. However, the symmetry does exist with simple variables as you can set them with the tag <c:set ...>

Can anyone give an overview on how to manage arrays with EL/JSTL and without scriptlet?

I saw the two following ways to do it by using scriptlets:

<jsp:useBean id="colors" class="java.util.ArrayList" scope="session">

<%

colors.add("red");

colors.add("green");

%>

</jsp:useBean>

and:

<% pageContext.setAttribute("colors", new String[] {"red","green"} , pageContext.SESSION_SCOPE);%>

In both cases, you access the array-values with the EL-expression: ${colors[0]}

jeromerga at 2007-7-14 16:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I haven't officially read this anywhere, but I have been taught that JSP's main purpose is *front end* and that it is best practice to use a JSP to only *display* data , and not modify data or add new data.

One would reserve adding new data, modifying and deleting data to the middle layer via JavaBeans , Java Classes and Data Access objects.

I think the JSTL and EL are limited in their abilities to allow one to perform CRUD operations (except for R - read) inside a JSP page on purpose.

By having this limitation, one is forced to implement the core logic in the middle layer, and only reserve the display part to the JSPs.

So, where ever it's possible to write the main logic inside the middle layer it should be avoided in the JSPs.

appy77a at 2007-7-14 16:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
<%@ taglib prefix="fn" uri=" http://java.sun.com/jsp/jstl/functions"%><c:set var="myArray" value="${fn:split('one,two,three',',')}" />
CodeMonkeea at 2007-7-14 16:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...