0

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!

Mike S.
  • 63
  • 4

1 Answers1

0

The script engine in Java provides the ability to execute Javascript code, but does not provide an emulation of a browser. Use HTTP clients like okhttp to download the raw HTML of the page.

For more information please visit “document” is not defined in java ScriptEngine

Mujahid Masood
  • 111
  • 2
  • 8
  • Hi and thank you for your response. If I have a getMethod to the url that I want to call will it execute it or it will just print its content? – Mike S. Mar 08 '18 at 16:40
  • You can use JSP scheduler and Servlets to achieve this functionality. – Mujahid Masood Mar 08 '18 at 16:45
  • That's exactly what I am using. The servlet that I try to redirect to holds a doGet method. If I understand correctly okttp prints the http section of the page, it does not redirect you to the actual page. – Mike S. Mar 08 '18 at 16:50
  • In the doGet you can first check if the date is today, and start scheduler and when timeout will occur just call response.sendRedirect("/toyoururl"); – Mujahid Masood Mar 08 '18 at 16:53
  • This is the first part that I implement. The second part stores the date in a database and executes the thread when the stored date is equal to Calendars day. This thread must then call my doGet to do everything... – Mike S. Mar 08 '18 at 17:09
  • Introduce a new Servlet and implement database logic in that and call that servlet using `RequestDispatcher rd = request.getRequestDispatcher("servlet2"); rd.forward(request,response);` – Mujahid Masood Mar 08 '18 at 19:04
  • Didn't know about that. The problem again with that is that the method that must be implemented is either a doPost or a doGet. I am trying to do that through an execute method that does not hold an HttpServletRequest request. – Mike S. Mar 08 '18 at 19:18
  • What does your execute method looks like? If its helper method you can invoke it from doGet as well. – Mujahid Masood Mar 08 '18 at 19:25
  • My execute method is the execute() method of Quartz scheduler. – Mike S. Mar 08 '18 at 19:28
  • You can easily call execute method from doGet or doPost. You can also use QuartzInitializerServlet or please have a look at this. https://www.splinter.com.au/using-quartz-scheduler-in-a-java-web-app-serv/ – Mujahid Masood Mar 08 '18 at 19:32
  • Ok. I'll have a look and update you with the results! Thank you for your time! – Mike S. Mar 08 '18 at 19:38