【问题标题】:Redirect to another servlets path through Java Method通过 Java 方法重定向到另一个 servlet 路径
【发布时间】:2018-03-08 15:56:13
【问题描述】:

我的 doGet 中有一个方法,如果日期不等于今天,它会启动一个线程。调度程序将找到 execute() 方法。在这个 method() 中,我尝试使用重定向。

方法一: 我使用 ScriptEngine 通过 Java 调用我的 js 方法,但它总是给我以下错误:

js方法:

function httpGet(theUrl){
window.open(theUrl)
}

Java 方法:

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);

错误:

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

方法二:

我尝试使用 HttpUrlConnection,但问题是它永远不会将我重定向到页面,即使它已连接。

 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();

问题是我在网上阅读的所有内容都指向通过 doGet 或 doPost 重定向。我想通过我的 execute() 方法重定向。

谢谢!

【问题讨论】:

    标签: javascript java servlets


    【解决方案1】:

    Java 中的脚本引擎提供了执行 Javascript 代码的能力,但不提供浏览器的模拟。 使用像 okhttp 这样的 HTTP 客户端来下载页面的原始 HTML。

    更多信息请访问 “document” is not defined in java ScriptEngine

    【讨论】:

    • 您好,感谢您的回复。如果我对要调用的 url 有一个 getMethod,它会执行它还是只打印它的内容?
    • 你可以使用JSP调度器和Servlets来实现这个功能。
    • 这正是我正在使用的。我尝试重定向到的 servlet 包含一个 doGet 方法。如果我理解正确,okttp 会打印页面的 http 部分,它不会将您重定向到实际页面。
    • 在 doGet 中你可以先检查日期是否是今天,然后启动调度程序,当超时发生时,只需调用 response.sendRedirect("/toyoururl");
    • 这是我实现的第一部分。第二部分将日期存储在数据库中,并在存储的日期等于日历日时执行线程。然后这个线程必须调用我的 doGet 来做所有事情......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多