Re: URL-escape a var with JSTL

Eric Haszlakiewicz <[email protected]>
Newsgroups gmane.comp.jakarta.taglibs.user
Message-ID <[email protected]>
> [email protected] wrote:
> >Hello,
> >I need to URL-escape a variable in JSTL, and am not having much luck 
> >finding the way to do it.
> >
> >For example, I currently do:
> ><c:set var="foo" param="${param.foo}"/>
> >
> >Since "foo" param may include some funky characters, I need to URL-escape 
> >that.  So I'm looking for something similar to:
> >
> ><c:set var="foo" param="${param.foo}" escapeXml="true"/>

<c:set var="foo"><c:out value="${param.foo}"></c:set>
or
<c:set var="foo" value="${fn:escapeXml(param.foo)}"/>

However, that's encoded with HTML/XML character entities, NOT url encoding.
i.e.
    input value  |   c:out   |    url encoding
    "foo bar"      "foo bar"      "foo+bar"
    "A&B"        | "A&amp;B" |    "A%26B"

> >What's the right way to URL-encode a variable with JSTL, without resorting 
> >to scriptlets?

	Note that URLEncoder.encode() is a static method, so if you really
do mean "url encode" you could write a short tld to allow you to access
it as an EL function.  Or, you could use the jakarta String TagLib, which
has a encodeUrl tag:
http://jakarta.apache.org/taglibs/doc/string-doc/string-1.1.0/index.html#encodeUrl

eric
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.