How to get the URL fragment identifier from HttpServletRequest

You can’t get the URL fragment in the way you’d like.

Typically, the browser doesn’t send the fragment to the server. This can be verified by using a network protocol analyser like tcpdump, Ethereal, Wireshark, Charles.

However, you can send the fragment string as a GET/POST parameter on a JavaScript request. To get the value using JavaScript, use window.location.hash. You can then pass this value as a GET or POST parameter in an AJAX request, and use the getParameter methods on the HttpServletRequest for the AJAX request.

Here’s what RFC3986: Uniform Resource Identifier (URI): Generic Syntax has to say:

The fragment identifier is separated
from the rest of the URI prior to a dereference, and thus the
identifying information within the fragment itself is dereferenced
solely by the user agent, regardless of the URI scheme. Although
this separate handling is often perceived to be a loss of
information, particularly for accurate redirection of references as
resources move over time, it also serves to prevent information
providers from denying reference authors the right to refer to
information within a resource selectively. Indirect referencing also
provides additional flexibility and extensibility to systems that use
URIs, as new media types are easier to define and deploy than new
schemes of identification.

Leave a Comment