1

like code:

public static void main(String[] args) throws Exception {
    String content="<HTML>"
     +"<HEAD><TITLE>title</TITLE></HEAD>"
     +"<BODY>"
     +"<script>var jsvar=123;</script>"
     +"</div>"
     +"</BODY>"
     +"</HTML>"
    ;
}

in this case,how to get jsvar variable value?

thanks for help :)

0

3 Answers 3

2

If you want to execute JavaScript code in Java, You can use scripting API of Java 6 and Java 6 is included with Mozilla Rhino engine.

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
jsEngine.eval("jsvar = 123");
System.out.println(jsEngine.get("jsvar")); //prints 123.0

Reference: http://download-llnw.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html

Sign up to request clarification or add additional context in comments.

Comments

0

Seems to me that this is backward. If you are serving the page then you already have the value on the server. I would inject the value from Java into JavaScript not the other way around.

Comments

0

In fact, you're asking something that requires a browser. As a consequence, you may have to take a look at HtmlUnit, which could solve your issue.

However, were you to directly run pure javascript code from Java, you would have better using a Javascript interpreter like Rhino (I think).

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.