java 在请求 dispatcher.forward 方法之后从另一个 servlet 调用一个 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25854077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a servlet from another servlet after the request dispatcher.forward method
提问by user3245747
I'm writing a web page using java servlets. When the user subscribes he will receive an email with the activation link. Currently the servlet redirects the user (using the request dispatcher) to the thank you page after it sends the email and this takes some time. I would like to redirect the user to the page before the email is created and sent and then have the email creation performed by another servlet without the user having to wait. Is this possible? How can I call a servlet from within another servlet after using the request dispatcher.forward method? Is that possible? If not, then what is the best way to do what I want? Thanks.
我正在使用 java servlet 编写网页。当用户订阅时,他将收到一封包含激活链接的电子邮件。目前,servlet 在发送电子邮件后将用户(使用请求调度程序)重定向到感谢页面,这需要一些时间。我想在创建和发送电子邮件之前将用户重定向到页面,然后让另一个 servlet 执行电子邮件创建,而无需用户等待。这可能吗?使用请求 dispatcher.forward 方法后,如何从另一个 servlet 中调用 servlet?那可能吗?如果没有,那么做我想做的最好的方法是什么?谢谢。
采纳答案by Hubert
To call another servlet from a servlet, you donot use request.getRequestDispatcher("xxx").forward(req,res);
but rather you use response.sendRedirect("servletname or path to the servlet");
要从 servlet 调用另一个 servlet,您不使用request.getRequestDispatcher("xxx").forward(req,res);
而是使用response.sendRedirect("servletname or path to the servlet");
You can also add parameters to the response.SendRedirect("xxx");
so that the recieving would use that parameters to sent the email.
To add parameters to the response.sendRedirect
response.sendRedirect("<servletname>?email="+email);
this will expose the user's email address with the new url.
您还可以向 中添加参数,response.SendRedirect("xxx");
以便接收方使用该参数发送电子邮件。要将参数添加到 response.sendRedirect,
response.sendRedirect("<servletname>?email="+email);
这将使用新 url 公开用户的电子邮件地址。
Based on your question, you can do something like this:
request.getRequestDispatcher("page/to/display/user").forward(req,res);
session.setAttribute("email","username of user");
response.sendRedirect("servlet/to/redirect");
根据您的问题,您可以执行以下操作:
request.getRequestDispatcher("page/to/display/user").forward(req,res);
session.setAttribute("email","username of user");
response.sendRedirect("servlet/to/redirect");
EDIT: You can create a method with boolean which will redirect the user to the thank you page, when successful, then process the email:
编辑:您可以创建一个带有布尔值的方法,该方法将用户重定向到感谢页面,成功后,然后处理电子邮件:
public boolean redirect(HttpServlet request, HttpServlet){
response.sendRedirect("<servletname>");
return true;
}
public boolean redirect(HttpServlet request, HttpServlet){
response.sendRedirect("<servletname>");
return true;
}
In the Servlet:
在 Servlet 中:
if(redirect){
processEmail(parameters);
}
END EDIT
if(redirect){
processEmail(parameters);
}
结束编辑
Then in the servlet that you want to send the email, you use the session
to get the email that was added to the response.
String email = (String) session.getAttribute("email");
然后在要发送电子邮件的 servlet 中,使用session
来获取添加到响应中的电子邮件。
String email = (String) session.getAttribute("email");
Hope this helps.
希望这可以帮助。
回答by Serge Ballesta
There are in fact 2 different problems in your question :
实际上,您的问题有两个不同的问题:
- you want to redirect to another page
- you want to continue processing after the redirection
- 你想重定向到另一个页面
- 您想在重定向后继续处理
It is simple to redirect to another URL from a servlet, just call :
从 servlet 重定向到另一个 URL 很简单,只需调用:
response.sendRedirect("url?param=value");
For the second part, it seems that it is possible to continue processing in servlet aftersending the response to the client. From my test on a Tomcat 7 servlet container, you can do so if ContentLength is set to 0 (as body is empty) and the output stream is closed. I could never have a confirmation from servlet specs, but as I wrote above it works on Tomcat :
对于第二部分,似乎可以在将响应发送到客户端后继续在 servlet 中处理。从我对 Tomcat 7 servlet 容器的测试来看,如果 ContentLength 设置为 0(因为主体为空)并且输出流已关闭,则您可以这样做。我永远无法从 servlet 规范中得到确认,但正如我上面所写的,它适用于 Tomcat:
response.sendRedirect("url?param=value");
response.setContentLength(0);
response.getOutputStream().close();
// continue after connection with client is closed
// generate and send email
回答by Gas
Based on your description you would like to return message to the user and then process 'in the background' email request. It shouldn't be done via other servlet. If you have Java EE server with JMS support, that is typical task for JMS and MDB. Your servlet would just put the message email sending request to the queue and returning message to the user. MDB would pick up that message and process it, and send the email.
根据您的描述,您希望向用户返回消息,然后在“后台”处理电子邮件请求。它不应该通过其他 servlet 来完成。如果您拥有支持 JMS 的 Java EE 服务器,那么这是 JMS 和 MDB 的典型任务。您的 servlet 只会将消息电子邮件发送请求放入队列并将消息返回给用户。MDB 将接收该消息并进行处理,然后发送电子邮件。
Method with spawning is doable as you tested and should work in most of servers, however it is discouraged. See some discussion here Why spawning threads in Java EE container is discouraged?
产生的方法在您测试时是可行的,并且应该在大多数服务器中工作,但是不鼓励这样做。请参阅此处的一些讨论为什么不鼓励在 Java EE 容器中生成线程?
In Java EE 7 you will have support for it via concurrency utilities - Concurrency Utilities tutorial
在 Java EE 7 中,您将通过并发实用程序获得对它的支持 - Concurrency Utilities 教程