如何从批处理程序中调用 servlet?

发布于 2024-11-17 20:07:14 字数 1125 浏览 2 评论 0 原文

我遇到过这样的情况:我有一系列类似的 JSP,每个 JSP 都是根据用户输入的选项从 servlet 调用的。

但是,我想调整这些 JSP,以便可以从服务器上每小时运行的程序中批量调用它们,并将 JSP 输出写入文本文件。

谁能告诉我如何做到这一点?

我的想法是:

URL url = new java.net.URL("http://127.0.0.1/myServlet");
URLConnection con = url.openConnection();

或者有更好的方法吗?

好的:我一定在这里做了一些非常愚蠢的事情,因为这似乎不起作用:我有一个每小时运行一次的批处理程序,它包含以下代码:

try {
        URL url = new java.net.URL("http://127.0.0.1:8084//myApp//myServletMapping?par=parValue");
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        connection.setDoInput(true);
        InputStream response = connection.getInputStream();
   }
   catch (Exception ex) {
        logger.error("Error calling servlet in batch", ex);
   }

根据我对 本教程,以上内容应该足以触发Servlet 中的 get 方法它是由上面代码中的 myServletMapping 映射到的。这个 servlet 的 get 方法包含一个简单的 System.out.println("Here");我希望看到这一点。

我做错了什么?

I have a situation where I have a series of similar JSPs, each of which is called from a servlet based upon an option entered by a user.

However, I would like to adjust these JSPs so that they can be additionally called in batch from a program which runs hourly on the server, and write the JSP output to text file.

Can anyone tell me how this might be done at all?

I am thinking along the lines of:

URL url = new java.net.URL("http://127.0.0.1/myServlet");
URLConnection con = url.openConnection();

Or is there a better way?

OK: I must be doing something very foolish here because this doesn't appear to work: I have a batch program which runs every hour and it contains the following code:

try {
        URL url = new java.net.URL("http://127.0.0.1:8084//myApp//myServletMapping?par=parValue");
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        connection.setDoInput(true);
        InputStream response = connection.getInputStream();
   }
   catch (Exception ex) {
        logger.error("Error calling servlet in batch", ex);
   }

According to my understanding of the instructions in this tutorial, the above should be enough to trigger the get method in the servlet which is mapped to by myServletMapping in the code above. This servlet's get method contains a simple System.out.println("Here"); which I would expect to see.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苦笑流年记忆 2024-11-24 20:07:14

或者有更好的方法吗?

并不真地。这就是最基本的了。当服务器接收到 HTTP 时,servlet 就会被“调用”,而这正是您建议的代码将要做的事情。

可以使用诸如HTTPUnit之类的库,或其他编程语言,但它是所有这些都归结为发送 HTTP 请求。

Or is there a better way?

Not really. That's about as basic as it gets. The servlet is "called" when the server receives an HTTP, and that's exactly what your proposed code will do.

You could use a library such as HTTPUnit, or a different programming language, but it's all going to boil down to sending an HTTP request.

旧伤慢歌 2024-11-24 20:07:14

除了 Matt 所说的关于发出 HTTP 请求时调用 servlet 的内容之外:

不需要从批处理文件中调用 Java 类。只需使用 wget 检索页面:

wget http://127.0.0.1/myServlet -O mypage.jsp

wget 是开源的并且可用于(几乎)所有操作系统

In addition to what Matt said about the servlet being called when you make a HTTP request:

There is no need to a Java class that is called from the batch file. Just use wget to retrieve the page:

wget http://127.0.0.1/myServlet -O mypage.jsp

wget is open source and available for (nearly) all operating systems

甜心小果奶 2024-11-24 20:07:14

我认为你的解决方案很好。我唯一要改变的就是不要自己实际阅读资源。我建议使用谷歌的 Resources.toString lib 像这样

String data = Resources.toString(url, ...);

I think your solution is fine. The only thing I would change is not actually do the reading of the resource your self. I would suggest using Google's Resources.toString lib like so

String data = Resources.toString(url, ...);
野心澎湃 2024-11-24 20:07:14

我不确定你问的意思是什么

我想调整这些JSP,以便可以批量额外调用它们

它们是否需要身份验证?

无论如何,根据您的要求,您甚至可以尝试使用 spring batch

http://static.springsource.org/spring-batch/

im not sure what you mean by asking

I would like to adjust these JSPs so that they can be additionally called in batch

do they need authentication ?

anyways, depending on your requirements you could even try it with spring batch

http://static.springsource.org/spring-batch/

笔落惊风雨 2024-11-24 20:07:14

您还可以查看 Apache HTTP 客户端库...同样,这可能是一个无法调用简单 Servlet 的库。

you could also look at Apache HTTP Client library... again this might be too much of a library to call a simple Servlet..

墨洒年华 2024-11-24 20:07:14

如果可能的话,您应该重构您的 servlet,并将逻辑移至不依赖请求/响应的服务类,以便您可以从任何您喜欢的地方调用它。

两周前我做了类似的事情 - 从网络应用程序生成的报告需要作为计划作业运行。我将所有代码从 Spring 控制器(它在那里做什么?)移到一个服务类中,并从控制器和预定的 Quartz 作业中调用该代码。

If possible you should refactor your servlets and move the logic out to a service class that doesn't rely on request/response so that you can call this from wherever you like.

I did something similar two weeks ago - a report that was generated from the web app needed to be ran as a scheduled job. I moved all the code from a Spring controller (what was it doing there anyway?) into a service class and called that code from the controller and a scheduled Quartz job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文