12

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

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

in the jsp, how would I convert that string into:

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

using

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

? I need to URLEncoder.encode(url) in the c:out somehow.

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
tester
  • 21,160
  • 23
  • 84
  • 122

2 Answers2

16

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

<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>

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:

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

with

public static String urlEncode(String value, String charset) throws UnsupportedEncodingException {
    return URLEncoder.encode(value, charset);
}
Community
  • 1
  • 1
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
0

Try this:-

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

Granted, this will only escape special characters from XML.

limc
  • 38,266
  • 19
  • 97
  • 142