• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Pasing scriptlet variable to javascript

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 6959
1317
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 6959
1317
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 6959
1317
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 67670
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic