如何按顺序执行多个 servlet?

发布于 2024-09-05 11:59:41 字数 761 浏览 2 评论 0原文

我刚刚开始使用 Servlet,并设法让一些 servlet 充当单独的 URL,用于填充数据库以进行一些虚拟测试。某种形式:

public class Populate_ServletName extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
     resp.setContentType("text/plain");
     //Insert records
     //Print confirmation
  }
}

我有大约 6 个这样的 servlet,我想按顺序执行它们。我正在考虑使用 setLocation 设置要重定向的下一页,但不确定这是否是正确的方法,因为重定向应该在插入记录之后发生。具体来说,我正在寻找这样的东西:

public class Populate_ALL extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
     resp.setContentType("text/plain");
     //Call Populate_1
     //Call Populate_2
     //Call Populate_3
     //...
  }
}

有什么建议吗?

I am just beginning with Servlets and managed to have some servlets that act as individual URLs for populating a database for some dummy testing. Something of the form:

public class Populate_ServletName extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
     resp.setContentType("text/plain");
     //Insert records
     //Print confirmation
  }
}

I have about 6 such servlets which I want to execute in a sequence. I was thinking of using setLocation to set the next page to be redirected but was not sure if this is the right approach because the redirects should happen after the records have been inserted. Specifically, I am looking for something like this:

public class Populate_ALL extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
     resp.setContentType("text/plain");
     //Call Populate_1
     //Call Populate_2
     //Call Populate_3
     //...
  }
}

Any suggestions?

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

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

发布评论

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

评论(2

若有似无的小暗淡 2024-09-12 11:59:41

使用 RequestDispatcher#include() 位于与 Servlet 的 url-pattern 匹配的 URL 上。

public class Populate_ALL extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("text/plain");
     request.getRequestDispatcher("/populateServlet1").include(request, response);
     request.getRequestDispatcher("/populateServlet2").include(request, response);
     request.getRequestDispatcher("/populateServlet3").include(request, response);
     //...
  }
}

注意:如果这些 servlet 不能独立使用,那么这是错误的方法,您应该为此使用不扩展 HttpServlet 的独立 Java 类。在您的具体情况下,我认为构建器模式可能会引起兴趣。

RequestDispatcher#forward() 在这里不合适,因为当响应标头已经提交时它会抛出 IllegalStateException 。当您通过多个 servlet 传递请求/响应(每个 servlet 都写入响应)时,无疑会出现这种情况。

HttpServletResponse#sendRedirect() 绝对不适合这里,因为它隐式创建了一个全新的 requestresponse,从而废弃了原始的。

另请参阅:

Use RequestDispatcher#include() on an URL matching the url-pattern of the Servlet.

public class Populate_ALL extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("text/plain");
     request.getRequestDispatcher("/populateServlet1").include(request, response);
     request.getRequestDispatcher("/populateServlet2").include(request, response);
     request.getRequestDispatcher("/populateServlet3").include(request, response);
     //...
  }
}

Note: if those servlets cannot be used independently, then this is the wrong approach and you should be using standalone Java classes for this which does not extend HttpServlet. In your specific case, I think the Builder Pattern may be of interest.

The RequestDispatcher#forward() is not suitable here since it throws IllegalStateException when the response headers are already committed. This will be undoubtely the case when you pass the request/response through multiple servlets which each writes to the response.

The HttpServletResponse#sendRedirect() is absolutely not suitable here since it implicitly creates a brand new request and response, hereby trashing the original ones.

See also:

记忆之渊 2024-09-12 11:59:41

看起来您可能需要的是每个 servlet 都可以用来执行某些工作的服务。这样,servlet 就不再相互依赖,而是全部使用该服务。

不过,这里有一个关于转发或重定向请求的解释。

It looks like what you may need is a service that each of the servlets can use to perform some work. Then the servlets are not depending one and another, but rather all using the service.

However, here is an explanation of forwarding or redirecting requests.

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