aspose file tools*
The moose likes JSP and the fly likes Pasing scriptlet variable to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Pasing scriptlet variable to javascript" Watch "Pasing scriptlet variable to javascript" New topic
Author

Pasing scriptlet variable to javascript

David Kennedy
Ranch Hand

Joined: Jan 22, 2006
Posts: 33
Hi all,

I need to use a variable from a scriptlet in some javascript and I cant get it working. My code is the following. Scriptlet where i set a request attribute followed by calling it in some javascript. Any ideas would be very welcome.

Thanks



"There are only 10 types of people in the world: Those who understand binary, and those who don't"
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3224
    
  49

<% request.getAttribute("variable"); %> is just calling the getAttribute method ignoring the returned value. If you need to get the returned value, you should call it as <%= request.getAttribute("variable") %>
However, note that using scriptlets on JSPs is obsolete - it's a bad practice to have Java codes in JSPs. You can easily use EL for this requirement, replacing all of these scriptlets with handy EL tags.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
David Kennedy
Ranch Hand

Joined: Jan 22, 2006
Posts: 33
Thanks for the reply Devaka,

My code now looks like this below but it still isnt working. am I still missing something?

<% String variable = "December 25, 2010";
request.setAttribute("variable", variable);
%>


<SCRIPT LANGUAGE="JAVASCRIPT">

var myVar = <%= request.getAttribute("variable") %>;
document.write(myVar);
</SCRIPT>

I have also tried

<SCRIPT LANGUAGE="JAVASCRIPT">

var myVar = <%= request.getAttribute("variable") %>
document.write(myVar);
</SCRIPT>

Thanks
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

My code now looks like this below but it still isnt working


Did you look at the html source on the browser and confirm that the variable is not being populated?
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
You are also missing the quote marks around your variable so the result currently looks like this

var myVar = December 25, 2010;

and that is invalid javascript. But you see this yourself when you look at the resulted html source.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3224
    
  49

David, please UseCodeTags when you post a code. It's unnecessarily hard to read the code otherwise. Please edit your post to add code tags by clicking the button.
David Kennedy
Ranch Hand

Joined: Jan 22, 2006
Posts: 33
Thanks for the help and the quotes fixed it.
Apologies for the lack of code thanks.

Here was the final code for anybody else:

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3224
    
  49

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

But you should take the other advice given to you earlier in this topic: scriptlets have been discredited for over 8 years now. Time to update your JSP knowledge.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
 
subject: Pasing scriptlet variable to javascript