Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4968
  • Last Modified:

c:set access variable

Hi, I have a JSP page and within the JSP code there is

<C:set var="itemList" value="${stff.stuff}"/>

In a javascript function I have above the code can I access the variable itemList.  BTW this jsp code is not inside a form.
0
corduroy9
Asked:
corduroy9
  • 5
1 Solution
 
bloodredsunCommented:
>>In a javascript function I have above the code can I access the variable itemList.  BTW this jsp code is not inside a form.

Yep, just put it inside a script
-----------
<C:set var="itemList" value="${stff.stuff}"/>
<script>
alert("my JSTL variable is: " + <c:out value='${itemList}'/>") ;
</script>
0
 
suprapto45Commented:
Hi,

Sure, why not?
First, make sure that ${stff.stuff} is not empty or null. Then you need to set the scope of the variable itself. Either it is in request, session or page or etc. <c:set var="itemList" scope="request" value="${stff.stuff}" />

Then surely you can call this variable from your JavaScript such as
onClick="javascript:location.href='/david.jsp?action=<c:out value="${requestScope.itemList}" /> '">

I hope that helps.

Regards
Dave
0
 
bloodredsunCommented:
Here's a slightly more coherent version
-----------------------
<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
<html>
<head>
<title>jstl2js</title>
</head>
<body>
<c:set var="itemList" value="Banana"/>
<script language="JavaScript">
alert("my JSTL variable is:  <c:out value='${itemList}'/>") ;
</script>
</body>
</html>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
corduroy9Author Commented:
bloodredsun... You are on the right track for what I'm looking for.

Inside of the <script> </script> I want to use the value of itemList.  How would I say assign a local Script variable to it.  Just var test = ${itemList} ??
Do I need the '' or anything else?

Thanks!
0
 
bloodredsunCommented:
>>Just var test = ${itemList} ??
Almost...

Just this:
var test = <c:out value='${itemList}'/> ;
0
 
bloodredsunCommented:
Although if it's a string you need to do this

var test = "<c:out value='${itemList}'/>" ;

:-)
0
 
bloodredsunCommented:
e.g.
-----------------
<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
<html>
<head>
<title>jstl2js</title>
</head>
<body>
<c:set var="test" value="Banana"/>

<script language="JavaScript">
var test = "<c:out value='${test}'/>" ;
document.write(test) ;
</script>

</body>
</html>
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now