Donotello
Donotello

Reputation: 157

How to execute javascript on a webpage using java

I am establishing a HttpUrlConnection with a webpage . Now I have javascript snippet that I want to execute on this webpage . How can I do this ?

Upvotes: 0

Views: 1933

Answers (2)

TheWalkingCube
TheWalkingCube

Reputation: 2116

You could try :

    private static ScriptEngineManager mgr = null;
    private static ScriptEngine engine = null;

    mgr = new ScriptEngineManager();
    engine = mgr.getEngineByName("JavaScript");

    Object eval = engine.eval(s); // s is javascript code

Upvotes: 2

Thomas Junk
Thomas Junk

Reputation: 5676

Since Javascript needs some kind of Virtual Machine to run in - usually provided by browsers or programms which act like browsers (so called headless browsers), you have to use some kind of "browsing engine" (so to say) to run the page in. Perhaps WebEngine may be worth a look.

Upvotes: 1

Related Questions