jQuery in Action, 2nd edition*
The moose likes JSP and the fly likes problem with ampersand(&) in passing values through URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "problem with ampersand(&) in passing values through URL" Watch "problem with ampersand(&) in passing values through URL" New topic
Author

problem with ampersand(&) in passing values through URL

archana vishwanath
Ranch Hand

Joined: Mar 09, 2004
Posts: 39
Hi!
This is the problem I face some times when using get method to pass
values from one page to other.If the values im trying to pass has some ampersands(&)
(e.g) say <a href="http://localhost/file2.jsp?value=aa<b rel="nofollow">&&&&&</b>aaa" target="_blank">http://localhost/file2.jsp?value=aa&&&&&aaa
purposely I have added some ampersands for value and im sure this will end up in problem
Is there any way to overcome this???
Deepak A
Ranch Hand

Joined: Oct 04, 2001
Posts: 120
u make that value as String.
I mean.
String s = a&&&a;
and then pass in the url as http://url?value=s;


Face Off.
velmurugan veerappan
Greenhorn

Joined: Apr 01, 2004
Posts: 3
hi,
i think this is not possible. because every query string key-value pairse
is parsed using &.

thanks,
velmurugan veerappan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Of course it is possible, and simply making it a string will do nothing for you.
All your URL values should be URL-encoded. You can easily accomplish this with the help of the java.net.URLEncoder class. This will encode special characters (such as the &) to a form that will not confuse the format of the URL.
[ April 01, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mishra Anshu
Ranch Hand

Joined: Sep 16, 2003
Posts: 224
Hello Bear,
Can you provide an exapmle or some snippet of the code for using
this encoder class.
On Google...there are lots....
A basic snippet would solve the purpose


"Ignorance is bliss"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

There's not much to it. For the example value in the original post:

will produce:

which is URL-safe.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with ampersand(&) in passing values through URL
 
Similar Threads
Repeat class method call 'new obj' question
pass params from jsp to another jsp help
location bar of the browser on include/forward/sendRedirect
Passing value from JSP to Cewolf Dataproducer Java File
Encoding ampersands in a URL