pass and ampersand

response.sendRedirect("showAddAMember?cntry=" + country +"&stte=" + state +"&city=" + city +"&course=" + course +"&coursename=" + coursename +"&messagename=" + messagename +"&TFM=" + worker +"&admin_type=" + admin_type);

I am using a response.sendredirect() to pass parameters from one servlet to another. Above is one such case I am using in my program. The problem I am having is that I have an ampersand (&) as part of a variable string I am passing. When the next servlet is called and the variables are passed, the '&' is reached in the content of the string and is recognized as the start of the next variable to be passed. Hence, any remaining part of the string is chopped off or severed right before the ampersand.

For example,

"...&messagename=The Raven G&CC&TFM=green&admin_type=4");

only 'The Raven G' gets passed instead of the entire string that I want passed, which is 'The Raven G&CC'.

What can I do to pass a string with an ampersand within it without it getting severed? As you can see the variable string content is variable, so it must not work just for a one time pass but be adaptable depending on the content of the current variable string.

There must be a java object that takes care of this problem or you could never pass a variable string with an ampersand in its content.

If anyone knows of the generally accepted method for solving this problem that would be of great help to myself. My program depends on it and can not operate without it.

Thank-You for your help.

Derek Z.

[1861 byte] By [derk_the_zeemana] at [2007-11-27 8:23:57]
# 1
Use reponse.encodeURL() for links and response.encodeRedirectURL() for use with response.sendRedirect(); that should do the trick.Also, note that these are different from encodeUrl() and encodeRedirectUrl() which are deprecated.
nogoodatcodinga at 2007-7-12 20:12:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I tried your suggestion but it did not work I still had the same problem.

Here is the code I tried;

response.sendRedirect(response.encodeRedirectURL("showAddAMember?cntry=" + country + "&stte=" + state + "&city=" + city + "&course=" + course + "&coursename=" + coursename + "&messagename=" + messagename + "&TFM=" + worker + "&admin_type=" + admin_type));

Anything after the ampersand got severed just like before.

Do you have a coded example that works that I could view.

derk_the_zeemana at 2007-7-12 20:12:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi, apparently I was wrong about encodeURL() and encodeRedirectURL() solving your problem, they are meant for writing the session data as part of the query string if cookies are not supported.

Try using the URLEncoder.encode() method as given here http://www.javapractices.com/Topic96.cjp.

Also, I think it is still advisable to keep the encodeRedirectURL() in the sendRedirect().

Hope I got it right this time :)

nogoodatcodinga at 2007-7-12 20:12:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I just tried out URLEncoder.encode() and it seems that if you pass it a URL which has the query string and so has ampersands already in it, it'll encode those too!

So you'll end up with a wierd URL that will not work, so don't pass it the whole string. Only pass the parameter values so that they are valid.

nogoodatcodinga at 2007-7-12 20:12:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...