wood burning stoves 2.0*
The moose likes Servlets and the fly likes Getting value of text after Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting value of text after "#" in request" Watch "Getting value of text after "#" in request" New topic
Author

Getting value of text after "#" in request

Michael J. Makunas
Ranch Hand

Joined: Mar 11, 2002
Posts: 37
If I have a URL like this:

http://www.foo.com/bar.jsp#baz

Is there a standard way to get the string "baz" from the request object without having to do pattern patching on the whole URL string?

-Michael
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, HttpServletRequest.getRequestURI will get you from the protocol name up to the query string.
From there, you're on your own.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Michael J. Makunas
Ranch Hand

Joined: Mar 11, 2002
Posts: 37
Originally posted by Ben Souther:
No, HttpServletRequest.getRequestURI will get you from the protocol name up to the query string.
From there, you're on your own.


Just so I'm clear on this....from playing arround with it myself and looking at the docs I've gathered that getRequestURI returns from the protocol to the the end of the query string and no more, correct? Does this mean the string "#baz" isn't even sent in the HTTP request?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Does this mean the string "#baz" isn't even sent in the HTTP request?

I think it is (someone may correct me here). But it is not part of the URI. In HTTP the # character is something called a fragment identifier. A "fragment" is an instruction to whatever user agent (typically a browser) made the request to perform an additional action after the request has been completed. It was intended as a way of allowing the user agent to give a partial view of the returned document.

Think about what happens when you click a link of this type:

The request is for http://www.somewhere.com, which is returned in its entirety, then the user agent refocuses the section of this document to the #section2 anchor. So the #section2 fragment is not important to the web server. As far as it is concerned, these two:

are identical requests for the same URI.

You might try HttpServletRequest.getRequestURL() - that might give you it (assuming #baz is not also considered part of the query string). Given the purpose of fragments though, why does your servlet need it?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Michael J. Makunas
Ranch Hand

Joined: Mar 11, 2002
Posts: 37
Originally posted by Paul Sturrock:

You might try HttpServletRequest.getRequestURL() - that might give you it (assuming #baz is not also considered part of the query string). Given the purpose of fragments though, why does your servlet need it?


Actually, I'm doing it from a jsp, but it should work the same. I want it so that I can track which fragments are being viewed more (i.e., which are more popular). (They are coming from another jsp...I know this won't work if the link is on the same jsp).

Unfortunately, HttpServletRequest.getRequestURL() doesn't seem to include "#baz". That's what leads me to believe it's not sent as part of the request and only the user-agent knows about it. The workaround is to put the fragment id in the query string too (e.g., foo.jsp?id=baz#baz). Not that much work but I was trying not to repeat info when I don't have to.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

When you click on anchor tags that point to the same page that your already on, there is no request made at all.

If you have Firefox or Mozilla installed on your computer, install the Live Headers plugin. Then go to a page with a lot of intra page links (such as the Java API docs). Click on the internal and external links and compare what the browsers sends.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting value of text after "#" in request
 
Similar Threads
Rewrite URLs
getParameter() in JSP always returns null...why?
JAX-ws client reporting my service is not a valid service! Help!!
Validate a given url against valid comma seperated list of domain names
difference between URL and URI