AJM
AJM

Reputation: 32490

HttpServletRequest - SetParameter

I know that I can use HttpServletRequest.getParameter() to get the URL parameter values.

Is there an equivalent method with which I can set/replace the value?

Upvotes: 11

Views: 38099

Answers (5)

Anand Kulkarni
Anand Kulkarni

Reputation: 87

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.

Upvotes: 1

BalusC
BalusC

Reputation: 1108732

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).

Upvotes: 6

Thilo
Thilo

Reputation: 262514

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.

Upvotes: 10

Drew Wills
Drew Wills

Reputation: 8446

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.

Upvotes: 4

MCory
MCory

Reputation: 437

No. However, why would you want to do that? There may be other ways of accomplishing what you need to do.

Upvotes: 3

Related Questions