1

I have an URL to encode on my java serveur and then to decode with javascript. I try to retrieve a String I send in param with java. It is an error message from a form validation function.

I do it like that (server side. Worker.doValidateForm() return a String) :

response.sendRedirect(URLEncoder.encode("form.html?" + Worker.doValidateForm(), "ISO-8859-1"));

Then, in my javascript, I do that :

function retrieveParam() {
    var error = window.location.search;

    decodeURIComponent(error);
    if(error)
        alert(error);
}

Of course it doesn't work. Not the same encoding I guess.

So my question is : which method can I use in Java to be able to decode my URL with javascript ?

Emilie
  • 668
  • 1
  • 9
  • 21
  • Possible duplicate of: http://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-output – Magnus H Jul 13 '16 at 10:06

2 Answers2

2

It's ok ! I have found a solution.

Server side with Java :

URI uri = null;
try {
    uri = new URI("http", "localhost:8080", "/PrizeWheel/form.html", Worker.doValidateForm(), null);
} catch (URISyntaxException e) {
    this.log.error("class Worker / method doPost:", e); // Just writing the error in my log file
}
String url = uri.toASCIIString();
response.sendRedirect(url);

And in the Javascript (function called in the onload of the redirected page) :

function retrieveParam() {
    var error = decodeURI(window.location.search).substring(1);

    if(error)
        alert(error);
}
Emilie
  • 668
  • 1
  • 9
  • 21
0

You don't use URLEncoder to encode URLs, it us used to encode form data to application/x-www-form-urlencoded MIME format. You use URIEncoder instead, see http://contextroot.blogspot.fi/2012/04/encoding-urls-in-java-is-quite-trivial.html

Endy
  • 698
  • 3
  • 11
  • I have used the URI object, like in your link, but it still doesn't work. I can't use URIEncoder because the URL is sent form Java. – Emilie Sep 05 '12 at 08:45
  • Try with javascript unescape() ? Provide more details on your case like more code, what errors you get, etc. – Endy Sep 05 '12 at 12:21