How to uri encode a string in jsp?

后端 未结 2 893
南方客
南方客 2021-02-05 20:35

if I have a string \"output\" that equals a url:

${output} = \"/testing/method/thing.do?foo=testing&bar=foo\"

in the jsp,

2条回答
  •  误落风尘
    2021-02-05 20:52

    It's not directly possible with standard JSTL tags/functions. Here's a hack with help of :

    
    
    

    URL-encoded component: ${url}

    If you want to do it more cleanly, create an EL function. At the bottom of this answer you can find a basic kickoff example. You'd like to end up as:

    URL-encoded component: ${my:urlEncode(output, 'UTF-8')}

    with

    public static String urlEncode(String value, String charset) throws UnsupportedEncodingException {
        return URLEncoder.encode(value, charset);
    }
    

提交回复
热议问题