2009-05-19 6 views

cevap

7

.

Javascript'te encodeURIComponent'dan kaçıyorsunuz, ancak size verdiğim Commons bileşeninin ihtiyaçlarınızı karşılayacağını düşünüyorum.

+0

encodeURIComponent kodunun kodunu çözmek istiyorsanız, buraya bakın http://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-outpu – reevesy

4

İstemci JavaScript/ECMAScript:

encodeURIComponent(cookie_value) // also encodes "+" and ";", see http://xkr.us/articles/javascript/encode-compare/ 

Sunucu Java:

String cookie_value = java.net.URLDecoder.decode(cookie.getValue()); 

Ben my blog entry daha da keşifler ekleriz.

1

En doğru yol, java kodunuzu içeren Excecute javascript'tir. Umarım aşağıdaki kod yardımcı olur.

ScriptEngineManager factory = new ScriptEngineManager(); 
    ScriptEngine engine = factory.getEngineByName("JavaScript"); 
    ScriptContext context = engine.getContext(); 
    engine.eval("function decodeStr(encoded){" 
      + "var result = unescape(encoded);" 
      + "return result;" 
      + "};",context); 

    Invocable inv; 

    inv = (Invocable) engine; 
    String res = (String)inv.invokeFunction("decodeStr", new Object[]{cookie.getValue()}); 
+0

Nashorn motor ile basitçe yazabilirsiniz: (String) invocable.invokeFunction ("unescape", escapedString); – Shafiul

0

Ortak lang'in StringEscapeUtils benim için çalışmadı.

Kaçak bir javascript dizesini çıkarmak için javascript nashorn motorunu kullanabilirsiniz.

private String decodeJavascriptString(final String encodedString) { 
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); 
    Invocable invocable = (Invocable) engine; 
    String decodedString = encodedString; 
    try { 
     decodedString = (String) invocable.invokeFunction("unescape", encodedString); 

    } catch (ScriptException e) { 
     e.printStackTrace(); 
    } catch (NoSuchMethodException e) { 
     e.printStackTrace(); 
    } 

    return decodedString; 
}