I'm using JSTL to develop a simple web application, but I'm having troubles linking to named anchors, e.g., linking to: http://localhost/myapp/foo.jsp#bar What I'd like to do is something like this: <c:redirect url="foo.jsp#${bar}"/> Where "${bar}" is some variable pointing to the named anchor to which I'd like to jump. The above <c:redirect> tag works ok until you turn on url rewriting, at which point it fails, sending a redirect instead to: http://localhost/myapp/foo.jsp#bar;jsessionid=JHFAKJHEUHFNNMEJHERREIIW:-1 But what it should do is generate a redirect to: http://localhost/myapp/foo.jsp;jsessionid=JHFAKJHEUHFNNMEJHERREIIW:-1#bar I guess I'd like to see a change to the JSTL <c:url> and <c:redirect> tags to support an anchor attribute, such as: <c:redirect url="foo.jsp" anchor="${bar}"/> or perhaps: <c:url value="foo.jsp"> <c:anchor value="${bar}"/> </c:url> or maybe both of the above. Alternatively, the JSTL could be made smart enough so that <c:url "foo.jsp#bar"> generates the correct URL, but this seems hackish to me. Perhaps the JSR-052 is the place to suggest this, I'm not sure. Thanks for a great tag library! It's saved me a ton of time.
This would indeed require a specification change, so it's best to mail the expert group's comments alias at jsr-52-comments@jcp.org. For the record, I actually think your suggestion to build the appropriate logic directly into the <c:url> and <c:redirect> tags is more appropriate than the <c:anchor> subtag you suggest as an alternative; I believe the relevant standards (e.g., RFC 2396) are clear that "#" always appears at the end of a URL, even when what the servlet spec calls "path parameters" (the ";jsessionid=...") are present.
I tested this on Tomcat and got the proper behavior. That is: <c:url value="foo#${bar}"/> yielded: foo;jsessionid=E61745F7208AD1FA63298454B16A8030#bar The issue of properly rewriting the URL is in fact the responsibility of HttpServletResponse.encodeURL(). You may want to ping your servlet container vendor on this.