Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes URL Encode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL Encode" Watch "URL Encode" New topic
Author

URL Encode

ford Darcy Jr
Ranch Hand

Joined: Jan 26, 2005
Posts: 76
Hi,

I am using URL encode in my servlet program to replace spaces. I need to change the + value with %20. Is there any way to do it since if I use URL encode its outputting "+" instead of "%20".

Thanks.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Originally posted by ford Darcy Jr:
Hi,

I am using URL encode in my servlet program to replace spaces. I need to change the + value with %20. Is there any way to do it since if I use URL encode its outputting "+" instead of "%20".

Thanks.

If you're using Java 1.4 or later, use String.replaceAll().

encoded = myString.replaceAll(" ", "%20");
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Maybe something like URLEncoder would do the trick?
[ May 18, 2005: Message edited by: Pauline McNamara ]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Originally posted by Pauline McNamara:
Maybe something like URLEncoder would do the trick?

[ May 18, 2005: Message edited by: Pauline McNamara ]


But that puts a '+' in place of a space. I think we're trying to get "%20" instead.

Ryan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

Why? The + character is the proper encoding.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
...depending on what he has in mind. If he's trying to generate URL's, then a URLEncoder probably really is the right thing to use. But then again, URLEncoder doesn't solve the problem explicitly stated in the original post.

However, it may just be that he wanted to change spaces to %20 for other reasons, saw the URLEncoder, tried it, and found that it was a bad fit for his application after all.

I was just assuming he knows what he wants to do even if he doesn't know how to do it and I don't know why he wants to. I also assume that if he tried URLEncoder and it changed spaces to +s, he would verify that +s are wrong for his purposes before posting the question.

Of course I could be wrong.

Ryan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URL Encode
 
Similar Threads
encoding URL in (href element of) JSP without scriptlets
encodeURL & encodeRedirectURL
Special characters changing to %something%
To Send Multilingual data cross context
how to encode the url in strut2