2

I have a variable called statements which I am iterating over and naming row

<c:forEach items="${statements}" var="row">

Can I now use this variable row in a scriptlet if I do something like

<% ArrayList<String> myRows = **** something here *** %>

What do I have to replace ** something here * with to be able to do this.

Note: I know in theory this is bad, as far as I can see the problem I have (which is more complicated that this, can only be solved this way.

Ankur
  • 50,282
  • 110
  • 242
  • 312
  • where have you stored `statements` , is it in session,request where ? – jmj Mar 15 '11 at 07:41
  • I don't really know, I am using the Stripes framework, I think it comes as part of the response and I can simply grab it using EL when I am in the JSP. – Ankur Mar 15 '11 at 07:43

2 Answers2

2

It's available as an attribute of the page, request, session or application scope. If the scope is known, just call getAttribute() on the scope of interest.

<%
    Object pageAttribute = pageContext.getAttribute("name");
    Object requestAttribute = request.getAttribute("name");
    Object sessionAttribute = session.getAttribute("name");
    Object applicationAttribute = application.getAttribute("name");
%>

Or if the scope is unknown, use PageContext#findAttribute(). It searches in subsequently the page, request, session and application scopes and returns the first match.

<%
    Object unknownScopedAttribute = pageContext.findAttribute("name");
%>

The above is also basically what EL is doing under the covers.


Unrelated to the concrete problem, this is definitely a workaround. If you elaborate in detail why need to do this, then we may be able to come up with real solutions instead of workarounds. In the meanwhile, read this thoroughly: How to avoid Java code in JSP files?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-2

simple answer: don't use scriptlets. complex answer: don't use scriptlets.

And that's all there is to it, really: don't use scriptlets.

If you knew how the JSTL code works, where it gets the ${statements} value from, you'd know how to use it in a scriptlet as well. But as you shouldn't use scriptlets, I'm not going to tell you any more, I'd only be leading you towards your doom and I don't intend to do that. Anyway, unless your ${statements} is a Collection<List<String>> your "row" local is never going to be a List<String> :)

jwenting
  • 5,505
  • 2
  • 25
  • 30