• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pasing scriptlet variable to javascript

 
David Kennedy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4595
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% 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.
 
David Kennedy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4595
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4595
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic