views:

477

answers:

5

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?

+3  A: 

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

MCory
I want to encode all the url parameters
AJM
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
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
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
Don't do String#getBytes() and so on. It's plain cumbersome. Check my answer in this topic.
BalusC
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
+1  A: 

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
+2  A: 

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
Yup, you can always wrap it although there can be be quite a few methods.
Steve Kuo
A: 

No. There is not any other method.

giri
Why are you repeating an already given comment/answer? Please add something new or just upvote the answer you agree with and move along :)
BalusC
No need to be harsh :P They probably posted their comments at the same time :)
MLefrancois
~45 minutes later is not the same time :)
BalusC
+3  A: 

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