0

Good Morning:

I am having several problems trying to receive a String from Java to JSP (Javascript inside).

Java File

String var = "Hello World!";

JavaScript (inside the JSP):

window.onload = function() { 

    loadData();
};    


function loadData() {

    document.getElementById('paragraph').innerHTML = "<%=var%>";

    alert(matr1); } 
}

But I received org.apache.jasper.JasperException: Cannot compile the class.

The rest of JSP and JavaScript is correct, I am trying only to fill the select with the text received in Java, I read other topics but nothing works.

Any help?

Thanks.

10
  • You are calling java method from javascript ? Commented Jun 30, 2017 at 8:32
  • No he tries to call a java String from JavaScript. In Android this works fine with JavaScript Interfaces. Commented Jun 30, 2017 at 8:34
  • how would JS know any thing about this java code ? If it is a servlet you could put it in a session attribute Commented Jun 30, 2017 at 8:35
  • I think the html file can load the String from java but the corresponding js not -> did not see it. Commented Jun 30, 2017 at 8:37
  • In other topics in StackOverflow it says that this works, but nothing works. Commented Jun 30, 2017 at 8:37

1 Answer 1

0

The <%= %> or the expression tag is used in jsp as a replacement for out.print() function of java.
You don't need to put double quotes around it to use it in java script, in your case if you have imported the java file into your jsp correctly then i think this should work:

document.getElementById('paragraph').innerHTML = <%=var%>;

Or

try using the variable name directly,

document.getElementById('paragraph').innerHTML = var;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.