Get anchor from URI
I'm writing a JSP/Servlet and I'm trying to get the the anchor part of the URI e.g:
How do I get my mark from my request? Obviously request.getParameter() doesn't work?
Any help would be welcome.
This isn't possible as clients don't send the "anchor part" to the server
As an example, here's the exact request that Chrome generated after submitting http://example.com/#foobar (recorded using Wireshark):
GET / HTTP/1.1 Host: example.com Connection: keep-alive User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/532.3 (KHTML, like Gecko) Chrome/22.214.171.124 Safari/532.3 Cache-Control: max-age=0 Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Encoding: gzip,deflate Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 If-None-Match: "b300b4-1b6-4059a80bfd280" If-Modified-Since: Tue, 15 Nov 2005 13:24:10 GMT
See, no #foobar. So there's no way a server application can read it.
Here's the important part from the mentioned RFC 1808:
Note that the fragment identifier (and the "#" that precedes it) is not considered part of the URL. However, since it is commonly used within the same string context as a URL, a parser must be able to recognize the fragment when it is present and set it aside as part of the parsing process.
I guess you want to make a bookmarkable Ajax web-application and you want to replace certain fragments of the DOM without reloading the full page. (Otherwise you could use query parameters.) As sfussenegger mentions, browser doesn't transmit 'anchor' to the server. The solution is on the client-side.
Unfortunately, it's two client-server roundtrips.
More on this at ajaxpatterns.