Incompatiable javascript's encodeURIComponent() and Java's URLEncoder.encode()

There is incompatibility between the javascript encodeURIComponent and java URLEncoder.encode method.

URLEncoder.encode (Java API for URLEncoder)
When  you encode a string using the URLEncoder.encode method following rules apply

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The special characters ".", "-", "*", and "_" remain the same.
  • The space character " " is converted into a plus sign "+".
  • All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.  

from the above rules, java URLEncoder.encode will treat [-a-zA-Z0-9._*-] as litterals .

JavaScript encodeURIComponent

     JavaScript's encodeURLComponenet [-a-zA-Z0-9._*~'()!] as litterals . and space will converts as "%20".

Following is implementation of javascript compatiable encoding in java

public static String encodeURIComponent(String originalStr){
    String encodeString= null;
    try{
      encodeString = URLEncoder.encode(s, "UTF-8") 
                         .replaceAll("\\+", "%20")
                         .replaceAll("\\%21", "!")
                         .replaceAll("\\%27", "'")
                         .replaceAll("\\%28", "(")
                         .replaceAll("\\%29", ")")
                         .replaceAll("\\%7E", "~");

    }catch (UnsupportedEncodingException e)    {
      System.out.println("Exception while encoding");
    }

    return encodeString;  } 


Another approach if you are using the JAVA 6, using the javascript engine that ships with JDK1.6

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public static String encodeURIComponent(String originalStr){
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        engine.eval("print(encodeURIComponent(originalStr))");
}


1 comment:

  1. Solution that worked for me based on last code snippet:
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;

    /**
    *
    * @author atharva
    */
    public class Testing {

    public static void main(String[] args) throws ScriptException {
    String text = "My name is Atharva. I am = human";
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    Object eval = engine.eval("encodeURIComponent(\"" + text + "\")");
    text = eval.toString();
    System.out.println("URI Component encoded text: " + text);
    }
    }

    ReplyDelete