• Post Reply Bookmark Topic Watch Topic
  • New Topic

issues between JSP and javascript

 
nene batungbakal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone!
i just have registered as of now... and i hope you could really help me in this one... i know you can!!
well, my problem is this:
i have made a JSP page with a javascript in it. i would like to ask if it is possible to put a
<% %> tag inside a <script></script> tag?
actually, i have already done this one before, i put a JSP variable with a value in it lets say
<% int JSPInteger = 10; %> inside a javascript tag to transfer that value into a javascript variable.
did you get what i said ? to put into computer terms it will look like this...

<% int JSPInteger = 10; %>
<script>
var JavascriptInteger;
JavascriptInteger = <% JSPInterger %>;
</script>
my question is can JavascriptInteger get the value of JSPInteger?
when i did this, an error occured saying JavascriptInteger was null or not a object..
is there any way to transfer the values of the variables from jsp to javascript and vice versa?
please help... no one can help me in this problem... btw, i also sori for my english grammars, im not really good in my english
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can be no real collaboration between JSP and JavaScript - the JSP code only generates HTML containing JavaScript. So, this is not in fact a JSP question but a JavaScript question. You need to stare at the resulting HTML+JavaScript and figure out what's wrong from there (I'd suggest using your browser's JavaScript console or similar).
 
Ravi Kumar Ravuru
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello nene batungbakal,
Please look at the below code....
<% int JSPInteger = 10; %>
<script>
var JavascriptInteger;
JavascriptInteger = "<% JSPInterger %>";
</script>
I had put the jsp scriptlet in quotes while assigning to javascript variable..try this....i wish it will work...
Ravi
 
Rajeshwari Natarajan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi's code will work perfectly. Whenever u want to access a JSP variable within javascript u access it as "<%=jspVariableName%>".
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
dont you miss the equals sign ?
<%= blabla %> instead of <% blabla %> should work.


k
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and:
if the server did not complain while compiling your JSP page to a servlet, then the JSP syntax was correct. this does not mean that the javascript you create through the JSP page is correct. if the javascript is faulty, then either it just doesn't work or the browser shows an error message (Javascript-Console in netscape/mozilla, error sign on bottom left in MSIE). As lasse mentioned: check out the resulting HTML code.
k
 
nene batungbakal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey! thanks a lot guys, the "<%= jspVariableName%>" really works, "" and = quotes really made a lot of difference... silly me...
btw, i checked out the html side of the jsp, and kaboom! it produced the value of the jspVariable... thanks.. hope you can still help me in my future problem..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!