java 如何将请求从 web1/servlet 转发到 web2/servlet?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4889113/
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
How to forward request from web1/servlet to web2/servlet?
提问by wasimbhalli
I've two web applications say web1 and web2. I want to forward a request from web1/servlet1 to web2/servlet2. Is it possible? Please help!
我有两个 Web 应用程序,分别是 web1 和 web2。我想将请求从 web1/servlet1 转发到 web2/servlet2。是否可以?请帮忙!
回答by skaffman
This is a two-step process:
这是一个两步过程:
- Get hold of the
ServletContext
representingweb2
- Get the
RequestDispatcher
from thatServletContext
corresponding toservlet2
- 抓住
ServletContext
代表web2
- 获得
RequestDispatcher
从ServletContext
对应servlet2
So, something like this, from inside servlet1
:
所以,像这样,从内部servlet1
:
ServletContext web1 = getServletContext();
ServletContext web2 = web1.getContext("/web2");
RequestDispatcher dispatcher = web2.getRequestDispatcher("/servlet2");
dispatcher.forward(request, response);
There's a big caveat to all of this - the container may not be configured to permit cross-context forwarding, since it's a potential security risk. If this is the case, getContext("web2")
will return null
.
所有这些都有一个很大的警告 - 容器可能未配置为允许跨上下文转发,因为它存在潜在的安全风险。如果是这种情况,getContext("web2")
将返回null
。