I have a method inside my doGet that starts a thread if the date is not equal to today. The scheduler will find the execute() method. Inside this method() I try to use the redirect.
Method 1: I used the ScriptEngine to call my js method through Java but it always gives me the following error:
Js method:
function httpGet(theUrl){
window.open(theUrl)
}
Java Method:
ScriptEngine engine = manager.getEngineByName("js");
// read script file
engine.eval(Files.newBufferedReader(Paths.get(url), StandardCharsets.UTF_8));
Invocable inv = (Invocable) engine;
// call function from script file
inv.invokeFunction("httpGet", "http://localhost:8080/myProject/StartScript?duration="+minutes);
Error:
javax.script.ScriptException: ReferenceError: "window" is not defined in <eval> at line number 3
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)
at watering.WateringScheduler.execute(WateringScheduler.java:99)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)Caused by: <eval>:3 ReferenceError: "window" is not defined
Method 2:
I tried to use HttpUrlConnection but the problem is that it never redirects me to the page even though it connects.
URL url;
log.info("hi "+ minutes);
url = new URL("http://localhost:8080/myProject/StartScript?duration="+minutes);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
int responseCode = urlConnection.getResponseCode();
System.out.println(responseCode);
if(responseCode==200) {
urlConnection = (HttpURLConnection) url.openConnection();
System.out.println("Redirect to URL : " + url);
}
BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
The problem is that everything I read online points to redirect through the doGet or doPost. I want to redirect through my execute() method.
Thanks!