Java 如何在jsp中对字符串进行uri编码?

Java 如何在jsp中对字符串进行uri编码?,java,jsp,jstl,Java,Jsp,Jstl,如果我有一个等于url的字符串“output”: ${output} = "/testing/method/thing.do?foo=testing&bar=foo" 在jsp中,如何将该字符串转换为: %2Ftesting%2Fmethod%2Fthing.do%3Ffoo%3Dtesting%26bar%3Dfoo 使用 <c:out value="${output}"/> ?? 我需要以某种方式在c:out中使用URLEncoder.encode(url)。试

如果我有一个等于url的字符串“output”:

${output} = "/testing/method/thing.do?foo=testing&bar=foo"
在jsp中,如何将该字符串转换为:

%2Ftesting%2Fmethod%2Fthing.do%3Ffoo%3Dtesting%26bar%3Dfoo
使用

<c:out value="${output}"/>

?? 我需要以某种方式在c:out中使用URLEncoder.encode(url)。

试试这个:-

<c:out value="${output}" escapeXml="true" />


当然,这只会从XML中转义特殊字符。

标准JSTL标记/函数无法直接实现。下面是一个借助于
的黑客程序:


正在寻找在c:out中实际进行uri编码的东西。。有什么想法吗?
<c:url var="url" value=""><c:param name="output" value="${output}" /></c:url>
<c:set var="url" value="${fn:substringAfter(url, '=')}" />
<p>URL-encoded component: ${url}</p>
<p>URL-encoded component: ${my:urlEncode(output, 'UTF-8')}</p>
public static String urlEncode(String value, String charset) throws UnsupportedEncodingException {
    return URLEncoder.encode(value, charset);
}