Alex Rivera | Logout

How to encode a String representing URL path with JSTL?

Asked 2011-02-19T22:32:34.193
32

What is the best way to URL-encode a String representing URL path (not request parameter) with JSTL?

<c:url value="/user/${user.name}"/>

According to any documentation I find, this should take care of it. But it does not. It encodes parameters beautifully (<c:url value="/user/${user.name}"><c:param name="section" value="employment 4u so good"/></c:url>) but I'm not passing any parameters. How can I safely encode a simple URL, like above, without fear of what ${user.name} could be?

Edit
Report

1 Answer

1

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

Follow these steps to configure your web application with this tag library:

  1. Copy the tag library descriptor file to the /WEB-INF subdirectory of your web application.
  2. Copy the tag library JAR file to the /WEB-INF/lib subdirectory of your web application.
  3. Add a element to your web application deployment descriptor in /WEB-INF/web.xml as said in the above link

To use the tags from this library in your JSP pages, add the following directive at the top of each page:

below is the example of usage in jsp:

<a href="str:decodeUrl>${URL}</str:decodeUrl)"/>
answered 2012-09-14T19:33:25.047

Your Answer