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 ?

1

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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.
Try with javascript unescape() ? Provide more details on your case like more code, what errors you get, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.