You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by og...@yahoo.com on 2006/03/01 08:40:37 UTC

URL-escape a var with JSTL

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

What's the right way to URL-encode a variable with JSTL, without resorting to scriptlets?
Or is the only option to use escapeXml="true" when doing c:out?

Thanks,
Otis



---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: URL-escape a var with JSTL

Posted by Eric Haszlakiewicz <er...@swapsimple.com>.
> ogjunk-taglib@yahoo.com 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

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: URL-escape a var with JSTL

Posted by og...@yahoo.com.
Hi,
True, but I'm not using  to construct my URLs, because my URLs are rewritten (mod_rewrite) and don't look like they are in the webapp context.  For example, if I have myapp.war, and URLs are normally under http://example.com/myapp/.... , then yes, c:url would work.  However, my links are rewritten, so I have http://example.com/people, etc....

So I construct URLs like this:
<a href="/something/${foo}">foo</a>

Thanks,
Otis

----- Original Message ----
From: Rich <rw...@xsmail.com>
To: Tag Libraries Users List <ta...@jakarta.apache.org>
Sent: Wednesday, March 1, 2006 6:27:26 AM
Subject: Re: URL-escape a var with JSTL

I think you just need the <c:url> tag.

-R

ogjunk-taglib@yahoo.com 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"/>
> 
> What's the right way to URL-encode a variable with JSTL, without resorting to scriptlets?
> Or is the only option to use escapeXml="true" when doing c:out?
> 
> Thanks,
> Otis
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org





---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: URL-escape a var with JSTL

Posted by Rich <rw...@xsmail.com>.
I think you just need the <c:url> tag.

-R

ogjunk-taglib@yahoo.com 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"/>
> 
> What's the right way to URL-encode a variable with JSTL, without resorting to scriptlets?
> Or is the only option to use escapeXml="true" when doing c:out?
> 
> Thanks,
> Otis
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org