6

In a Java web project, how can I get (if possible) the "HTTP anchor" part in a URL request? For example, when the request URL is http://localhost:8080/servlet/page.htm?param1=value1&param2=value2#section I want to be able to recognize the #section part.

public void doGet
(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException 
{
  // ...
  System.out.println("QueryString = " + request.getQueryString());
  // ...
}

The example above produces the substring preceeding the #, in this case: param1=value1&param2=value2. Is there a way to extract the part of the URL after the # sign?

If this is of any use, I'm using the Apache Click framework.

jbatista
  • 2,747
  • 8
  • 30
  • 48
  • 1
    looks like a dupe of http://stackoverflow.com/questions/932184/how-do-i-get-url-label-when-parsing-request-url – Brad Cupit Nov 11 '11 at 23:27

1 Answers1

12

I don't think the anchor part is send to the server. It is only processed by the browser. Maybe you can extract it using JavaScript.

According to RFC 1808:

Note that the fragment identifier (and the "#" that precedes it) is not considered part of the URL.

From http://iwebdevel.com/2009/06/10/javascript-get-anchor-from-url/

var url=window.location;
var anchor=url.hash; //anchor with the # character  
var anchor2=url.hash.substring(1); //anchor without the # character
Jacob
  • 41,721
  • 6
  • 79
  • 81
  • 1
    you're right, it's not sent to the server: http://stackoverflow.com/questions/1637211/jsp-servlet-anchor/1637244#1637244 – Brad Cupit Nov 11 '11 at 23:27