11

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AJM
  • 32,054
  • 48
  • 155
  • 243

5 Answers5

10

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Yup, you can always wrap it although there can be be quite a few methods. – Steve Kuo Feb 23 '10 at 16:56
  • 1
    Old question but I'd like to add that a use case for doing this is creating a unit or functional test. So, it this case, can be done via using a Mock library or using a provided request stub that contains a modifiable parameter map. – Josef.B Jul 29 '15 at 15:09
6

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
4

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.

Drew Wills
  • 8,408
  • 4
  • 29
  • 40
3

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

MCory
  • 437
  • 2
  • 13
  • Before it gets to the servlet? You'll want to look into JavaScript for that -- once it gets to the servlet, there's no real point in trying to encode them anymore. Or are you talking about encoding it for a redirect to another servlet/page? If that's the case, you'll want to look into encoding it while you're building your new URL. Look into java.net.URLEncoder (http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLEncoder.html); might help. – MCory Feb 23 '10 at 16:17
  • I don't think he's talking about URL encoding. The request parameters are already URL-decoded by the servletcontainer. He's more talking about character encoding. – BalusC Feb 23 '10 at 17:55
  • Character encoding would be a different beast altogether. That could be done with the String.getBytes method, but apparently there's some pitfalls on that (I've never personally had to worry about character encoding in the work I've done, so I'm not a good one to ask on that). A quick Google search gave me http://illegalargumentexception.blogspot.com/2009/05/java-rough-guide-to-character-encoding.html. About 3/4 the way down there's a section called "The potential pitfalls of encoding and decoding using the String class" which will have more info, but the full doc seems like a good fit. – MCory Feb 23 '10 at 18:07
  • Don't do String#getBytes() and so on. It's plain cumbersome. Check my answer in this topic. – BalusC Feb 23 '10 at 20:01
  • I agree with BalusC -- if that is what you're trying to do, there has to be a better way than getBytes. If BalusC's answer doesn't suit your needs, check around for some decent libraries -- http://commons.apache.org/lang/ (Apache Commons Lang) has some support for working with different character encodings, and I'm sure there's many more libraries that will suffice as well. EDIT: BalusC's article is really good and seems to cover the bases pretty well in this respect. (I don't have the rep point to add comments to someone else's answer yet, or else I would; already voted it up though). – MCory Feb 23 '10 at 21:36
1

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.