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 repl开发者_运维知识库ace ** 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.
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?
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>
:)
精彩评论