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

Java string object value to javascript

 
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Am trying to pass the java string object value to javascript. But getting unknown variable temp exception.

Can I use Pagecontext to type cast ?
JSP page
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this :



i haven't checked it myself, but i think it should work ...
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No difference.. Please assist
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my mistake,


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure that any double quotes inside of "temp" are properly escaped.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your original code created JavaScript code that is looking for JavaScript variable temp. Salvin's code replaces the JavaScript variable temp with String literal "temp". You will need the following:
This will add the value of your Java variable temp to the JavaScript code as a String literal.

Please note that code can cause JavaScript errors. If temp contains line breaks, single quotes or double quotes then your JavaScript will be invalid. You can solve this by replacing the single and double quotes with ' and " respectively, and \n with 
 (line feed) and \r with 
 (carriage return). You should do this in Java code already:
Alternatively you can decide to replace all line breaks with <br>, the HTML code for line break.


Wow, beaten by two guys! Excellent!
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot use a java variable within a scriplet in a java script!!

eclose the variable declaration within a <script> tag as shown below... haven't executed the code but it should work!


 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't as a variable itself, but you can use its value as Salvin's code (and mine after that) has shown. Your code does almost the same but first creates a JavaScript variable with that value which is then used afterwards. Your code is flawed though; just try your code when the Java temp variable contains "Hello World":
That doesn't look like valid JavaScript, does it? You really do need the quotes around it, and also escape the characters as I described. Otherwise "Hello\nWorld" would lead to this:
Again, not valid JavaScript code.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Pradeep.v wrote:We cannot use a java variable within a scriplet in a java script!!

eclose the variable declaration within a <script> tag as shown below... haven't executed the code but it should work!




John,

Thanks for your time. But no luck... Am getting below error

 
John Pradeep.v
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you pleas post your JSP here? let me have a look at it..

John
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Salvin / Rob / Ulf,

Thanks a lot..... The below code helps me



 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Pradeep.v wrote:We cannot use a java variable within a scriplet in a java script!!


We can, We have.
 
John Pradeep.v
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:We can, We have.



Hi Savlin,
We can use it to construct the java script code in the servlet/jsp, but not within a java script once the response comes back to the browser...
so i meant use the java variable to construct the java script variable...

John
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pleaseeeeeeeeeeeee

S A L V I N, not savlin

my statement stands the same : We can
or rather:

We can use a java variable within a scriptlet in a java script



If you have any doubts that it cannot be used, you can execute the above example. (see the source of the generated page)

once response comes back to the browser its static code.
There isn't much difference in using a variable vs appending directly, unless you are using this variable multiple times in javascript but that's a design issue.


 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[ UD: Code removed, as it's broken. Rob's post explains why. Please, people, read and understand previous posts before posting. ]
 
John Pradeep.v
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:Pleaseeeeeeeeeeeee

S A L V I N, not savlin



SALVIN,
I think we both seem to say the same thing.. probably my statement was a bit confusing.. but the initial piece of code posted which has a problem will definetly not work (as the error message is very obvious), it is not able to find the variable temp as its very much visible not created in the scriplet... (or rather, not generated)

John



 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic