如何按顺序执行多个 servlet?
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
RequestDispatcher#include()
位于与 Servlet 的url-pattern
匹配的 URL 上。注意:如果这些 servlet 不能独立使用,那么这是错误的方法,您应该为此使用不扩展
HttpServlet
的独立 Java 类。在您的具体情况下,我认为构建器模式可能会引起兴趣。RequestDispatcher#forward()
在这里不合适,因为当响应标头已经提交时它会抛出IllegalStateException
。当您通过多个 servlet 传递请求/响应(每个 servlet 都写入响应)时,无疑会出现这种情况。HttpServletResponse#sendRedirect()
绝对不适合这里,因为它隐式创建了一个全新的request
和response
,从而废弃了原始的。另请参阅:
Use
RequestDispatcher#include()
on an URL matching theurl-pattern
of the Servlet.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 throwsIllegalStateException
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 newrequest
andresponse
, hereby trashing the original ones.See also:
看起来您可能需要的是每个 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.