0

I have a code that looks like this:

In AnalyzeUserClient.jsp:

<c:set var="arrayList"><%= (ArrayList<String>)request.getSession().getAttribute("arrayList") %></c:set>
var sessionId = [];
<c:forEach items=${arrayList} var="id">
sessionId.push("${id}"); // add them one at a time, assuming string values
</c:forEach>

However,this line:

    sessionId.push("${id}");

does not seem to be passing the values into the array "sessionId" (I viewed the source code on browser).So the question is,what can I do to pass the values into the array?

EDIT:I just realised that there are some problems as JSTL is server-side and JavaScript is client-side.So is there a workaround on it?

2
  • Regarding your edit about server-side working together with client-side, if the intention is to produce JS that populates the sessionId array and then use it only on the client-side then that doesn't matter. (It only becomes a problem if your server-side code tries to use the array too.) You should be able to use server-side code to output JS that populates an array - though I'd consider structuring it in a way that puts the values directly in the array literal [] rather than using .push(). Commented Nov 2, 2012 at 6:36
  • I tried doing: "sessionId["${id}"];" as well,but it is still not working.Is there another way to resolve this? Commented Nov 2, 2012 at 6:41

1 Answer 1

8

Don't mix EL and scriptlets. In fact, forget about scriptlets completely:

var sessionId = [];
<c:forEach items="${sessionScope.arrayList}" var="id">
    sessionId.push("${id}"); 
</c:forEach>

Note though that this will generate invalid JavaScript if one of the IDs happens to contain a double quote. So you'd better JavaScript-escape the IDs before, in your controller. And I would suggest a completely different approach: serialize the list of IDs to a JSON string in your controller, and store this JSON string in request attribute. The JSP page will just need

var sessionId = ${jsonEncodedSessionIds};

which will translate to the following generated code:

var sessionId = ["id1", "id2"];
Sign up to request clarification or add additional context in comments.

7 Comments

On the browser the line:"sessionId.push("${id}");" becomes "sessionId.push("");",is it related to the problems of server and client-side coding?
No. It just means that the session ID stored in your list is null or is an empty string.
If that is the case,why does the browser unable to read the JSTL array values into the javascript array?
I don't understand your question. sessionId.push("") is valid JavaScript. It adds an empty string to the sessionId array.
It does add the values of the JSTL list. But this list contains a null element or an empty string. If it shouldn't contain that, then you have a bug in the Java code which populates this list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.