Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

passing object from JSP to Javascript  RSS feed

 
Rajeshwari Ramamurthi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am trying to pass the java object from JSP to the javascript inside the same page.
It is not working.
Is there any other way to do this?
see the sample code

<% String s= "Hello" %>
<Script>
alert (<%= s %> ;
</script>
It is giving error in alert statement as
Error: Hello is undefined
Can anyone help me?
Thank you
Raji
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should look at the HTML sent to the browser to try and diagnose problems like this. What you will find is that your javascript is being generated as:

so, of course, it's trying to interpret Hello as a variable.
What you really want in your JSP is something like:

hth,
bear
[ March 20, 2002: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More on the matter:
The wording of your subject line leads me to suspect that you may have some basic misconceptions regarding how JSP works.
In your example, you are not "passing" the String object s to your Javascript function. Rather, JSP is a template language that merely formats a perfectly normal HTML page for sending to the browser (yes, it can be used for other than HTML, but that's not my point, and not relevant to this example).
Once the page is processed and sent to the browser, it is interpreted just as any other page is. Thus the Java objects in your JSP page exist only on the server, and cannot be "passed" to Javascript executing on the browser.
That distinction makes an enourmous difference to your understanding of how to write JSP. Your example:

would seem to make sense if you thought of this as "passing" String variable s to the alert() method, which expects a (Javascript) string object.
However, what <%= s %> really means is: while the text of the page to be sent to the browser is being constructed, replace the <%= s %> construct (including its delimiters) with the value of the enclosed expression expressed as a string.
Therefore, if you want the value of your Java String object s (with value "Hello") to be expressed as a Javascript string with the same value, the fragment:

creates a Javascript string literal in the response text sent to the browser.
There's a big difference between those two views on what is happening, and a good understanding of how the servlet container processes JSP pages is very helpful in avoiding such pitfalls.
Two mechanism that are extremely helpful in either debugging or understanding JSP is to:
1) Look at the Java source for the servlet created by the servlet container for the JSP page.
2) View the source code sent to the browser as a result of processing the JSP page.
hope this helps further,
bear
[ March 21, 2002: Message edited by: Bear Bibeault ]
 
Rajeshwari Ramamurthi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing out my mistake
I have two questions.
1. Where can I find the servlet code generated for my JSP page?
2. I have to pass a java object to my javascript in JSP page. So that I can display certain attributes of java object depends on user interaction with the web page.
Is it possible to pass the java object to javascript?
Is there any other way to do this ?
Thanks
Raji
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where the servlet engine writes the java files for the JSP servlets is discussed here.
As for your second question. Please re-read my second response and you'd understand that that's just not possible.
The servlet that represents the JSP executes on the server and its only job it to create a chunk of text that will be sent to the browser. By the time the Javascript in your page is executing on the client machine, the Java objects on the server in your JSP have long since gone out of scope and have probably been garbage collected.
What you can do is to create Javascript objects that contain the essence of what you need from your Java objects by generating the code to create them in your page.
Let's say, for example, that you have a typical address object on the server that contains address info, and that this info is needed on the client side as a Javascript object. (Exactly why is a good question).
You could do something along the lines of:

This allows you to approximate the java object as a javascript object on the client side.
That all said, I'd take a step back and think about your statement:

I have to pass a java object to my javascript in JSP page

Why? You might want to think about what you are trying to accomplish (or post it here). There's probably a much simpler, JSP-esque way to do what you are trying to do.
hth,
bear
[ March 21, 2002: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!