URL encode a string before redirecting it

I want to URL encode a string before I sendRedirect it.

What must I do? I have tried this but it did not work.

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

The URLEncoder.encode is deprecated so I can not use it or it is not wise to do so. What must I do to URL encode a string before redirecting it. I have written a simple method which temporarily takes care of this for me, but I want to use the accepted standard. Please somebody tell what I must do.

[922 byte] By [derk_the_zeemana] at [2007-11-27 10:07:32]
# 1
I believe the new version of encode takes two parameters, e.g.String encodedValue = URLEncoder.encode(text, "UTF-8");
billybeara at 2007-7-13 0:43:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> I want to URL encode a string before I sendRedirect

> it.

>

> What must I do? I have tried this but it did not

> work.

>

> response.sendRedirect(response.encodeRedirectURL

> ("showAddAMember?century=" + country + "&stte=" +

> state + "&city=" + city + "&course=" + course +

> "&coursename=" + coursename + "&messagename=" +

> messagename + "&TFM=" + worker + "&admin_type=" +

> admin_type));

response.encodeRedirectURL() and .encodeURL() do not replace unsafe URL characters; I'm assuming that's what you want happening; they're meant to encode the session id in the URL for session tracking.

Quote from http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletResponse.html#encodeRedirectUrl(java.lang.String):

Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.

For robust session tracking, all URLs emitted by a servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.

Like mentioned above, use the URLEncoder.encode() ( with two params ): http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLEncoder.html#encode(java.lang.String,%20java.lang.String)

nogoodatcodinga at 2007-7-13 0:43:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...