*
The moose likes Java in General and the fly likes Java string object value to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java string object value to javascript" Watch "Java string object value to javascript" New topic
Author

Java string object value to javascript

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
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
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

try this :



i haven't checked it myself, but i think it should work ...


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
No difference.. Please assist
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

my mistake,


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41144
    
  45
Make sure that any double quotes inside of "temp" are properly escaped.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Pradeep.v
Ranch Hand

Joined: Jul 21, 2008
Posts: 59
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Oct 08, 2008
Posts: 492
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

Joined: Jul 21, 2008
Posts: 59
Can you pleas post your JSP here? let me have a look at it..

John
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Salvin / Rob / Ulf,

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



salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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

Joined: Jul 21, 2008
Posts: 59
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
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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.


ashu Suri
Greenhorn

Joined: Oct 22, 2008
Posts: 18
[ 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

Joined: Jul 21, 2008
Posts: 59
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
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java string object value to javascript
 
Similar Threads
Servlet API 2.2
useBean
JSP & Javascript?
servlets-urgent pl reply
Famous "Object expected"error,Wishing Browser Independent