Square brackets in URL causes exception in Tomcat

I noticed the following exception is thrown in my Play! application running on Tomcat:

javax.servlet.ServletException: java.net.URISyntaxException: Illegal character in path at index 35: /K-NAAN/lyrics/Is-Anybody-Out-There[]-lyrics/3807088
    play.server.ServletWrapper.service(ServletWrapper.java:157)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.net.URISyntaxException: Illegal character in path at index 35: /K-NAAN/lyrics/Is-Anybody-Out-There[]-lyrics/3807088
    java.net.URI$Parser.fail(Unknown Source)
    java.net.URI$Parser.checkChars(Unknown Source)
    java.net.URI$Parser.parseHierarchical(Unknown Source)
    java.net.URI$Parser.parse(Unknown Source)
    java.net.URI.<init>(Unknown Source)
    play.server.ServletWrapper.parseRequest(ServletWrapper.java:241)
    play.server.ServletWrapper.service(ServletWrapper.java:132)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

According to this thread square brackets aren't valid characters in an URI, and it's the client's responsibility to encode them? I get that exception in Chrome on MacOS, but not on Firefox, which seems to imply Chrome doesn't encode them. What's worse, I'm pretty sure googlebot also encounters that exception.

What's the right course of action here?

Can I tweak Tomcat to somehow accept or encode square brackets? Or is it a bug or behavior in Play?

Or should I just make sure that every single link to my pages has square brackets URL-encoded? Possible on my own website, of course, but not so much for external links.


Any link that wants to use characters that may not be used directly in a URL or have a special meaning needs to encode the characters in the URL.

To make life just a little more complicated, I have noticed that FireFox often displays the unencoded characters but sends the encoded characters in the address bar. It is worth using something like Firebug (or the equivalent for your browser) to make sure you know what is actually being sent when trying to debug this sort of issue.


The specific exception we were encountering seems to be a bug in Play 1.2.4, and should be fixed in 1.2.5 by returning code 400 instead of 500.

https://groups.google.com/d/msg/play-framework/165UJ8LZ0e8/sD0na-NSxLUJ

We'll also go through and double-check that all our links encode square brackets (which they already should).

链接地址: http://www.djcxy.com/p/12098.html

上一篇: URL和URI之间的区别?

下一篇: URL中的方括号会导致Tomcat中的异常