Java 如何在转发时将请求参数从一个 servlet 传递到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21882485/
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 pass a request parameter from one servlet to a other while forwaring
提问by Gabriel Glenn
There is my goal that I can't achieve for now :
我现在无法实现的目标是:
I have one servlet, say 'ReportServlet'. It takes a request parameter, say 'p'. I can obviously get the parameter by :
我有一个 servlet,比如“ReportServlet”。它需要一个请求参数,比如“p”。我显然可以通过以下方式获取参数:
request.getParameter("p");
The query string in my JSP is :
我的 JSP 中的查询字符串是:
<a href="<c:url value="/report"/>?p=value">report</a>
And everythings works fine.
一切正常。
Now : I have another servlet, say 'PreProcessingServlet'. I want to forward PreProcessingServlet to ReportServlet, passing a 'p' parameter which is computed in PreProcessingServlet. I tried :
现在:我有另一个 servlet,比如“PreProcessingServlet”。我想将 PreProcessingServlet 转发到 ReportServlet,传递一个在 PreProcessingServlet 中计算的“p”参数。我试过 :
RequestDispatcher rd = getServletContext().getRequestDispatcher("/report?p="+value);
rd.forward(request, response);
But the parameter 'p' goes in the request's queryString member, not in the parameters.
但是参数 'p' 在请求的 queryString 成员中,而不是在参数中。
How can I pass the 'p' parameter, using query parameter in the way that I can retrieve 'p' the same way from the JSP and from the forward.
如何使用查询参数传递 'p' 参数,以便我可以从 JSP 和向前以相同的方式检索 'p'。
I don't want to use a request attribute because I want a unique solution to get the parameter from both a JSP and a forward.
我不想使用 request 属性,因为我想要一个独特的解决方案来从 JSP 和 forward 获取参数。
I guess I'm missing something, but I can't find what !
我想我错过了一些东西,但我找不到什么!
采纳答案by Sotirios Delimanolis
When in doubt, always go to the specification. In this case, see chapter 9.1.1 Query Strings in Request Dispatcher Paths
如有疑问,请始终查看规范。在这种情况下,请参阅章节9.1.1 Query Strings in Request Dispatcher Paths
The
ServletContext
andServletRequest
methods that createRequestDispatcher
objects using path information allow the optional attachment of query string information to the path. For example, a Developer may obtain aRequestDispatcher
by using the following code:String path = "/raisins.jsp?orderno=5"; RequestDispatcher rd = context.getRequestDispatcher(path); rd.include(request, response);
Parameters specified in the query string used to create the
RequestDispatcher
take precedence over other parameters of the same name passed to the included servlet. The parameters associated with aRequestDispatcher
are scoped to apply only for the duration of the include or forward call.
使用路径信息创建对象的
ServletContext
和ServletRequest
方法 允许将查询字符串信息可选地附加到路径。例如,Developer 可以 使用以下代码获取 a :RequestDispatcher
RequestDispatcher
String path = "/raisins.jsp?orderno=5"; RequestDispatcher rd = context.getRequestDispatcher(path); rd.include(request, response);
在用于创建的查询字符串中指定的
RequestDispatcher
参数优先于传递给包含的 servlet 的其他同名参数。与 a 关联的参数的RequestDispatcher
范围仅适用于包含或转发调用的持续时间。
So you can very well do
所以你可以很好地做到
RequestDispatcher rd = getServletContext().getRequestDispatcher("/report?p="+value);
rd.forward(request, response);
And the parameter p
will be available only for HttpServletRequest
that is given to the resource mapped to handle the specified path, ie. /report
in this case. If that is a HttpServlet
, you can then access it with
并且该参数p
仅适用HttpServletRequest
于被映射到处理指定路径的资源,即。/report
在这种情况下。如果那是HttpServlet
,则可以使用
request.getParameter("p");
where request
would be the HttpServletRequest
method parameter.
其中request
将是HttpServletRequest
方法的参数。
When the forward(..)
call terminates and execution comes back to your PreProcessingServlet
, the parameter will no longer be available in the local HttpServletRequest
object.
当forward(..)
调用终止并且执行返回到您的 时PreProcessingServlet
,该参数在本地HttpServletRequest
对象中将不再可用。