| Author |
HttpServlet Request getParameter automatic converting
|
Jeremy Linzer
Greenhorn
Joined: Aug 31, 2004
Posts: 8
|
|
I am having the following issue with getParameter method of HttpServletRequest. I have a JSP which is a for musing POST method. One of the form elements is a radio button that has an ASCII value for a comma, , for its value. For example, <input type="radio" name="radioName" value="Yes, more than 60 days ago" />Yes, more than 60 days ago. When I do a request.getParameter("radioName"); on this the , is converted into an actual comma. Any ideas where exactly this is happening and why and is there anything I can do about this. This happens both on Tomcat and WSAD 5.0? Thanks
|
 |
Jessica Sant
Sheriff
Joined: Oct 17, 2001
Posts: 4313
|
|
Hey Jeremy- Welcome to the JavaRanch! We've got the perflect place for servlet-y questions like this. So I'm going to move this to the Servlets forum. Please continue the conversation there. As for your question --> Check out the URLDecoder class. I think it will do the kind of thing you need. It's happening so that the character doesn't get corrupted. The standard format for URL's with parameters is like this: blah.com/somepage.jsp?param1=value1¶m2=value2 But what if the value you were sending actually contained a "&" like "Bonnie & Clyde" That could get pretty messy, your request would look like this: blah.com/somepage.jsp?hero=Davey+Crocket&villain=Bonnie+&+Clyde How would the application server know that the "&" inside "Bonnie+&+Clyde" was actually part of the value and not a marker to deliniate the next set of name / value pairs?? The solution is to encode all the param names and values with those funky %XX codes so no one gets confused. Make Sense?
|
- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
|
 |
Jeremy Linzer
Greenhorn
Joined: Aug 31, 2004
Posts: 8
|
|
Thanks for your reply. I want to correct something in my original posting The radio button code that I am using is actually <input type="radio" name="radioName" value="Yes, more than 60 days ago" />Yes, more than 60 days ago. The , is getting converted to a regular comma when I do a request.getParameter as in elementValue = request.getParameter(pageElementName);. I tried using the URLDecoder as you suggested and it still didn't work. I am using JDK 1.3 (I have no control over it). as in: elementValue = request.getParameter(pageElementName); String decoded = URLDecoder.decode(elementValue); Any other ideas
|
 |
Jessica Sant
Sheriff
Joined: Oct 17, 2001
Posts: 4313
|
|
o -- so you WANT it to stay in the format "Yes, more than 60 days ago" -- and NOT be converted into "Yes, more than 60 days ago" -- right? I think getParameter() converts things into the normal non-URL encoded format as a convenience.... So, if you'd like to access the raw data, use: request.getQueryString() -- but then its your responsibility to break it up into parameter names and values. But that should be pretty doable with the String.split() method. [ September 01, 2004: Message edited by: Jessica Sant ]
|
 |
Julian Kennedy
Ranch Hand
Joined: Aug 02, 2004
Posts: 823
|
|
|
Or you could presumably just re-encode it with java.net.URLEncoder.encode(elementValue, "UTF-8"), but then you'd get the pluses for spaces, etc. If you don't want the whole thing encoding you could just use String.replace() for the comma.
|
 |
Jeremy Linzer
Greenhorn
Joined: Aug 31, 2004
Posts: 8
|
|
|
Thanks for your responses. THey were a big help
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: HttpServlet Request getParameter automatic converting
|
|
|