如何在一个servlet中调用另一个servlet?

发布于 2024-10-09 00:28:51 字数 60 浏览 1 评论 0原文

正如标题提到的,如何在 servlet 中调用另一个 servlet 并获取调用的 servlet 响应?

As title metioned, How to invoke another servlet in a servlet and get the invoked servlet response?

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

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

发布评论

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

评论(4

︶ ̄淡然 2024-10-16 00:28:51

使用 RequestDispatcher 实例可通过 HttpServletRequest 实例获得。

但是,如果您希望获取 servlet 容器所持有的单个实例 [例如使用 ServletContext 实例中的 getServlet 方法],这是一个完全不同的故事。 Servlet 规范有意弃用了可能允许此类选项的操作。但是,如果您确实想要在执行另一个 servlet 的同时调用一个 servlet,请使用 RequestDispatcherinclude 方法,而不是 转发方法。

Use RequestDispatcher instance which is available through the HttpServletRequest instance.

However, if you're looking at getting hold of the single instance held by the servlet container [like using the getServlet method in the ServletContext instance], it's an entirely different story. The servlet specs have purposefully deprecated the operation which might allow such an option. But, if you really want to invoke one servlet while executing the other one, use the include method of the RequestDispatcher instead of the forward method.

小清晰的声音 2024-10-16 00:28:51

请参见此处:

getServletContext().getNamedRequestDispatcher("servletName")
    .forward(request, response);

不过我认为还有更好的选择。例如,将您需要的代码移动到辅助类/实用程序方法中,然后调用它。

我想起来,您可能还想要另一件事:单独调用 servlet。为此,您需要:(

InputStream is = new URL(urlOfTheServlet).openStream();
IOUtils.copy(is, response.getOutputStream());

这是使用 apache commons-io 将输入流复制到当前请求的输出流)

See here:

getServletContext().getNamedRequestDispatcher("servletName")
    .forward(request, response);

However I'd assume there are better options. For example move the code you need to a helper class / utility method, and invoke it.

As I come to think of it, you may want another thing: call a servlet separately. For that you need:

InputStream is = new URL(urlOfTheServlet).openStream();
IOUtils.copy(is, response.getOutputStream());

(this is using apache commons-io to copy the input stream to the output stream of the current request)

美人骨 2024-10-16 00:28:51

使用ServletContext或者当前请求获取RequestDispatcher,然后使用RequestDispatcher的forward()或者include()。

可以使用 Spring MockHttpServletRequest 和 MockHttpServletResponse 创建新的请求和响应,而不是使用当前请求。

例子:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
RequestDispatcher dispatcher = request.getRequestDispatcher(url);

MockHttpServletRequest servletRequest = new MockHttpServletRequest();
servletRequest.setServerName(request.getServerName() );
servletRequest.setServerPort(request.getServerPort() );
servletRequest.setSession(request.getSession() );
servletRequest.setMethod(HttpMethod.GET.name() );

servletRequest.setRequestURI(url);
servletRequest.setParameters(parameters);

MockHttpServletResponse servletResponse = new MockHttpServletResponse();
servletResponse.setCharacterEncoding("UTF-8");
// Use include() instead of forward(). Similar as client HttpClient GET
dispatcher.include(servletRequest, servletResponse);
String content = servletResponse.getContentAsString();

Use ServletContext or current request to get RequestDispatcher, and then use RequestDispatcher forward() or include().

Can use Spring MockHttpServletRequest and MockHttpServletResponse to create new request and response instead of use current request.

Example:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
RequestDispatcher dispatcher = request.getRequestDispatcher(url);

MockHttpServletRequest servletRequest = new MockHttpServletRequest();
servletRequest.setServerName(request.getServerName() );
servletRequest.setServerPort(request.getServerPort() );
servletRequest.setSession(request.getSession() );
servletRequest.setMethod(HttpMethod.GET.name() );

servletRequest.setRequestURI(url);
servletRequest.setParameters(parameters);

MockHttpServletResponse servletResponse = new MockHttpServletResponse();
servletResponse.setCharacterEncoding("UTF-8");
// Use include() instead of forward(). Similar as client HttpClient GET
dispatcher.include(servletRequest, servletResponse);
String content = servletResponse.getContentAsString();
秋意浓 2024-10-16 00:28:51
String destinationBlockAccount  ="./BlockAccount";
response.sendRedirect(response.encodeRedirectURL(destinationBlockAccount));

此外,您可以直接从 JSP 发送参数:

response.sendRedirect(response.encodeRedirectURL("./GetAccount?accountID="+accountID));
String destinationBlockAccount  ="./BlockAccount";
response.sendRedirect(response.encodeRedirectURL(destinationBlockAccount));

Furthermore you can sending a parameter like directly from JSP:

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