java - How to uri encode a string in jsp? -
if have string "output" equals url:
${output} = "/testing/method/thing.do?foo=testing&bar=foo"
in jsp, how convert string into:
%2ftesting%2fmethod%2fthing.do%3ffoo%3dtesting%26bar%3dfoo
using
<c:out value="${output}"/>
? need urlencoder.encode(url) in c:out somehow.
it's not directly possible standard jstl tags/functions. here's hack 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 want more cleanly, create el function. @ bottom of this answer can find basic kickoff example. you'd end 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); }
Comments
Post a Comment