Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

String to htmlFriendly string

Thread view: 
tiewknvc9 - 05 Jan 2008 22:43 GMT
Hi!

I was wondering if anyone knows if there is a simple way to make a
string html friendly within the address bar?

What I mean is that my servlet has a string with odd characters
+,-,*,^,etc and I am concerned that attaching it to the url (as in
www.mydomain.com?uid=dude&pwd=dkdf8&%*#dfj) will cause the link to
break.

So I am trying to figure out how to most easily convert the "pwd"
field to an url friendly string, and back again so that the next page
could use it.

Thanks!!!
Mark Space - 05 Jan 2008 22:52 GMT
> Hi!
>
[quoted text clipped - 11 lines]
>
> Thanks!!!

http://java.sun.com/j2se/1.4.2/docs/api/java/net/URI.html

I haven't checked this carefully but I believe that this will do what
you want:

URI uri = new URI( "insert crazy characters here" );
Wayne - 06 Jan 2008 07:02 GMT
> Hi!
>
[quoted text clipped - 11 lines]
>
> Thanks!!!

AFAIK there is no built-in way.  There are methods to encode
URLs (URIs) but they fall short.  This was discussed a month
ago in a different thread, with proper encoding code posted.
Look for the thread title containing "URL Encoding".  (I
saved a copy and can repost or email it if requested.)

You mentioned the address bar so it is likely you mean URL or
URI encoding, not HTML encoding.  But changing text to legal
HTML is easy.  You can use regular expressions (use global
and multi-line modes) on a String to:

  1) change all "&" to "&"
  2) change all "<" to "&lt;"
  3) change all ">" to "&gt;"

-Wayne
tzvika.barenholz@gmail.com - 06 Jan 2008 07:40 GMT
> Hi!
>
[quoted text clipped - 10 lines]
>
> Thanks!!!

ASF's commons lang package has a class called StringEscapeUtils that
handles all that.
see http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/StringEscapeUtils.html
Tzvika
Roedy Green - 06 Jan 2008 10:39 GMT
On Sat, 5 Jan 2008 14:43:27 -0800 (PST), tiewknvc9
<aotemp@hotmail.com> wrote, quoted or indirectly quoted someone who
said :

>What I mean is that my servlet has a string with odd characters
>+,-,*,^,etc and I am concerned that attaching it to the url (as in
>www.mydomain.com?uid=dude&pwd=dkdf8&%*#dfj) will cause the link to
>break.

see http://mindprod.com/products.html#AMPER  to convert & to &amp;
even in URLs.

http://mindprod.com/jgloss/products1.html#ENTITIES to convert < to
&lt; and back.

You can also strinp tags such as <div>
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Jean-Baptiste Nizet - 06 Jan 2008 15:00 GMT
tiewknvc9 a écrit :
> Hi!
>
[quoted text clipped - 9 lines]
> field to an url friendly string, and back again so that the next page
> could use it.

What you're looking for is the class java.net.URLEncoder.
You must use this on every parameter value of the URL.

To encode strings into an HTML page, use the Jakarta commons
StringEscapeUtils class.

JB.

> Thanks!!!
David Segall - 07 Jan 2008 06:09 GMT
>Hi!
>
[quoted text clipped - 9 lines]
>field to an url friendly string, and back again so that the next page
>could use it.
I faced this problem a few days ago and, as usual, Roedy Green's site
had the answer.
<http://mindprod.com/jgloss/urlencoded.html>
phoenixatsh@gmail.com - 07 Jan 2008 07:52 GMT
yes, Jakarta commons StringEscapeUtils class can help you or write the
same function code by yourself.
Daniel Pitts - 07 Jan 2008 20:02 GMT
> Hi!
>
[quoted text clipped - 10 lines]
>
> Thanks!!!

What you appear to want is a URL encoded string, not an HTML encoded
string.

Look at the java.net.UrlEncoder class.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.