如何依次执行多个servlet? [英] How do I execute multiple servlets in sequence?

查看:101
本文介绍了如何依次执行多个servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
  }
}

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

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
     //...
  }
}

有什么建议吗?

推荐答案

使用 RequestDispatcher#include() 在与 url-pattern 匹配的 URL 上的 Servlet.

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);
     //...
  }
}

注意:如果这些 servlet 不能独立使用,那么这是错误的方法,您应该为此使用不扩展 HttpServlet.在您的具体情况下,我认为 Builder Pattern 可能很有趣.

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.

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

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.

HttpServletResponse#sendRedirect() 绝对不适合这里,因为它隐式地创建了一个全新的 requestresponse,从而破坏了原创.

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

这篇关于如何依次执行多个servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆