Java HttpServletRequest - 设置参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2319638/
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
HttpServletRequest - SetParameter
提问by AJM
I know that I can use HttpServletRequest.getParameter()
to get the URL parameter values.
我知道我可以HttpServletRequest.getParameter()
用来获取 URL 参数值。
Is there an equivalent method with which I can set/replace the value?
是否有等效的方法可以设置/替换该值?
采纳答案by Thilo
No, there is not.
不,那里没有。
You can only change attributes, not parameters.
您只能更改属性,而不能更改参数。
The only way to achieve something similar would be to wrap the request (with a class that returns something else for getParameter).
实现类似目标的唯一方法是包装请求(使用一个为 getParameter 返回其他内容的类)。
Related curiosity: There is a bug in some servlet containers that would let you do request.getParameterValues(name)[0] = "newValue"
, but this can only lead to inconsistencies.
相关好奇心:在某些 servlet 容器中有一个错误可以让您这样做request.getParameterValues(name)[0] = "newValue"
,但这只会导致不一致。
回答by MCory
No. However, why would you want to do that? There may be other ways of accomplishing what you need to do.
不。但是,您为什么要这样做?可能还有其他方法可以完成您需要做的事情。
回答by Drew Wills
I don't think there is. But you can use the setAttribute()method in a similar fashion; you just have to use getAttribute() -- not getParameter() -- to get the value back later.
我不认为有。但是您可以以类似的方式使用setAttribute()方法;您只需要使用 getAttribute() —— 而不是 getParameter() —— 来稍后取回值。
回答by BalusC
You can make the parametermap a modifiable map by replacing the HttpServletRequest
with a custom HttpServletRequestWrapper
implementation which replaces the parametermap inside a Filter
which is been placed early in the chain.
您可以通过将 替换HttpServletRequest
为自定义HttpServletRequestWrapper
实现来使参数映射成为可修改的映射,该实现替换Filter
放置在链早期的a中的参数映射。
However, this smells like a workaround. In one of the comments you stated that you wanted to encode the parameters (actually: decodethem, because they are alreadyencoded). You're looking in the wrong direction for the solution. For GET
request parameters the encoding needs set in the servletcontainer itself (in case of for example Tomcat, just set URIEncoding
attribute of the HTTP connector). For POST
, you need to set it by ServletRequest#setCharacterEncoding()
. Also see the detailed solutions in this article(read the whole article though to understand the complete picture).
但是,这听起来像是一种解决方法。在您表示要对参数进行编码的评论之一中(实际上:解码它们,因为它们已经被编码)。您正在寻找解决方案的错误方向。对于GET
请求参数,编码需要在 servletcontainer 本身中设置(例如 Tomcat,只需设置URIEncoding
HTTP 连接器的属性)。对于POST
,您需要通过 进行设置ServletRequest#setCharacterEncoding()
。还看到详细的解决方案这篇文章(阅读全文虽然了解完整的图片)。
回答by Anand Kulkarni
Request parameters are submitted to a servlet or JSP from a client via HTTP. They are not set by server-side code so there is no need for any set methods (setParameter()).
请求参数从客户端通过 HTTP 提交到 servlet 或 JSP。它们不是由服务器端代码设置的,因此不需要任何设置方法 (setParameter())。
Also, it will add security that no one can change request parameters.
此外,它将增加安全性,没有人可以更改请求参数。