• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

passing the value of a parameter from java to javascript

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I am doing:

I have a JSP in whhich I display rows for a table (picking the data up from the database). This code for generating the table rows is in a java class. After its displayed, I want to make a select on a row and delete it. What I am tryin to do here is that I am generationg an id and linking that id with each row. This id comes back as a request parameter to the same jsp. I am trying to get the ids value in a javascript fiunction on the jsp. But it dosesn seem to work.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does "it doesn't work" mean? How are you trying to get the value into javascript? Without details it's impossible to even begin to guess what you are trying to do or what the issue may be.
 
author
Posts: 4342
40
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer the question in your topic (because I don't follow your post) you can 'convert' a JSP variable (lets use a java.lang.String as an example) to a Javascript variable very easily:

Then, anywhere in the javascript, just call myJavaScriptVariable and if the page is finished loading, the variable will be available.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:

Then, anywhere in the javascript, just call myJavaScriptVariable and if the page is finished loading, the variable will be available.




That's only going to work for very basic value. In general you need to escape you Java variable before assigning its value to the javascript var. Don't forget that the assignment happens by literally printing the Java var's value to the page.

As a starting point you could use the following code:



then in your bean, do something like:



and finally, on the JSP page:



of course, for numbers you don't use the single quotes.
 
Your buns are mine! But you can have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic