Share vars between scriptlet and jstl?

R

Robert Mark Bram

Hi All!

Can variables be shared between 'contexts' like this?


<%
Table i = new Table();
%>
<c:set var="noRows" value="${i.noRows}" />
<c:set var="noCols" value="${i.noCols}" />


Alternatively, how would I instantiate a Table object with a JSTL tag?

Rob
:)
 
R

Ryan Stewart

Robert Mark Bram said:
Hi All!

Can variables be shared between 'contexts' like this?


<%
Table i = new Table();
%>
<c:set var="noRows" value="${i.noRows}" />
<c:set var="noCols" value="${i.noCols}" />


Alternatively, how would I instantiate a Table object with a JSTL tag?
JSTL operates on variables stored in one of the four contexts: page, request,
session, and application. Simply declaring a variable does not place it in any
of those scopes. To do so, you invoke the setAttribute method on the object
corresponding to whichever scope in which you want to store the variable. The
objects are pageContext, request, session, and application. So to make your
Table available to JSTL, use:
<%
Table i = new Table(); // What kind of name is "i"?
pageContext.setAttribute("i", i);
%>

That will store a reference to the object referenced to by variable i in the
page scope with a name of "i", making it accessible to JSTL. You could also use
request.setAttribute("i", i);

or session.set... or application.set...

Better to get all scriplets out of your JSPs and do that kind of thing in a
controller of some sort.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JSTL: getting a map's keys 19
JSTL 0
JSTL config question 10
JSTL 1
Beyond trivial JSTL 1
JSTL concatenate 2
need help in JSTL/STRUTS + Custom Tag 3
problem upgrading to JSTL 1.2 11

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top