HttpServletRequest - SetParameter
I know that I can use HttpServletRequest.getParameter()
to get the URL parameter values.
Is there an equivalent me开发者_运维问答thod with which I can set/replace the value?
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).
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.
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.
However, this smells like a workaround. In one of the comments you stated that you wanted to encode the parameters (actually: decode them, because they are already encoded). 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).
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.
No. However, why would you want to do that? There may be other ways of accomplishing what you need to do.
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()).
Also, it will add security that no one can change request parameters.
精彩评论