Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know there is already questions about that, but I just can´t simply get this work, I have a JSP file with a java variable in it:

String test = "Hello";

And I need to read this value in the Javascript embedded in the same JSP file, I tried so many options, but it is not working, and for security I don't want pass the value using the URL or hidden values.

Any ideas of how get this working? Thanks :)

----UPDATE----

I changed my mind, I can use hidden fields to pass the value, now what I have is a hidden field called Id and I set its value:

value="<%=request.getAttribute("Id")%>"

And in Javascript I try to receive the value:

var Id = document.getElementById("Id").value;

But it is still not working :(

share|improve this question
add comment

2 Answers

var jsvariable="<%=test%>";
share|improve this answer
 
I already tried that: var jsvariable="<%=test%>"; But it doesn´t work :( –  user2596007 Jul 18 at 15:41
 
what does "does not work" mean? what is the output HTML? post the full JSP please. –  dbanet Jul 18 at 15:43
 
The variable that I try to pass always is received empty, In other words document.getElementById("Id").value; does not return anything –  user2596007 Jul 18 at 16:16
 
@user2596007 please, provide the whole JSPage. –  dbanet Jul 18 at 16:20
add comment

I know this one is old, but this worked for me:

var javaScriptVar="<%out.print(javaString);%>";

you need to make sure that if you are using an external js file (out of the jsp file), the above line has to be before the "include" script tag. for example this is the jsp file:

var javaScriptVar="<%out.print(javaString);%>";
<script src="some_location/test.js"></script>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.