It's not a secret anymore!
The moose likes Servlets and the fly likes Encoding URL parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Encoding URL parameters" Watch "Encoding URL parameters" New topic

Encoding URL parameters

Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
I'm passing some encoded string as a request parameter.
I suppose the right way to do this in a JSP file is
<input type="hidden" name="userId" value="<%=URLEncoder.encode("ENCRYPTED STRING HERE")%>">
Should I always use URLDecoder to decode the message?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Just using request.getParameter automatically decodes it. I am not sure why you are even calling the URLEncoder method on it though.

So much trouble in the world -- Bob Marley
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Just to make it clear, URLEncoder does not encrypt a value, merely make sure that it contains no troublesome characters for a URL: ' ' is replaced by '+', other non-alphanumeric characters are replaced by a hexadecimal character code in the form "%AB".
You only need to use URLEncoder if you are constructing a URL from scratch as a string, and it is quite rare to need to use URLDecoder as that is doen for you by the Servlet API.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
Yes. I'm manually concating an url link to be printed on a jsp page.
Some browser versions will cause the link to be linked wrongly if the request parameters are not encoded.
Turlapati Hyma

Joined: Jul 23, 2002
Posts: 2
Originally posted by Chu Tan:
I'm passing some encoded string as a request parameter.

(edited by Cindy)
Why don't you experiment with UBB in "Meaningless Drivel" or "Blatant Advertising" and not clutter up the high traffic forums.
[ July 23, 2002: Message edited by: Cindy Glass ]
I agree. Here's the link:
subject: Encoding URL parameters
It's not a secret anymore!