Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Variables from JSTL to JSP  RSS feed

Bloo Barton
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I access my variable that is defined in JSTL from standard JSP? How about the opposite (from JSP to JSTL)?

I tried the following

<c:set var="a" value="5" />

Here I will try to output a :: <%= a%>.

That would give me an error that 'a' is not defined.
Yes if I output it using JSTL
<c ut value="${a}" />

That of course would work fine.
Also, is it possible for me to embed HTML tags as part of a variable so I can dynamically generate forms?

For example, what I would like to do is something like this...

<c:set var="varX" value="<option value=""" />
<!-- not sure if "" is the correct way to escape a quotation?, this is how I would do it in ASP; I'm still new to JSP though... -->

<c:set var="varY" value="${a}" />
<c:set var="varZ" value=""">" />

<c ut value="${varX}${varY}${varZ}" />
Some Text Here

That is the basic idea of want I want to do, but the above code does not actually work...
Bear Bibeault
Author and ninkuma
Posts: 65826
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the whole purposes of the EL and JSTL is to eliminate scripting in the pages, so it's no surprise that they don't play along well together. When you "set" a variable using <c:set/> you are not creating a scripting variable, but rather a scoped variable.

Now, you can retrieve the scoped variable from its appropriate scope using the getAttribute() method and store it in a scripting variable, but I'd question why you are mixing JSTL/EL and scripting on the same page.

Quotes are escaped in EL expressions in the same way that they are in Java expressions, with the "\" character.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!