Monday, January 10, 2011

URL Formation and the Browser Interpretation

Recently I faced a issue where the URL formation was something like this : -

"http ://<server:port>/<context>/something.jsp#anchorValue"

and we were supposed to get the value of "anchorValue" from the URL.

We tried a various method exposed by the ServletRequest and its child classes: -

1. getQueryString()
2. getRequestURI()
3. getPathInfo()
4. getPathTranslated()
5. getRequestURL()

But none of the above method could give us the value after "something.jsp".

It seems that "#anchorValue" was totally ignored by the server

After carefully analyzing the URL interpreted by the Browser and the URL received by the server, we found that browser itself strips of few of the characters from the URL, before it sends to the server and "#" (and its) preceding values is one such case.

"#" and the content preceded by "#", is stripped off from the URL by the browser and the rest of the URL is sent to the server for further processing

Browser understands "#" as the internal Link to an HTML document (received as a part of response from the server)

Let's assume that we have the Following URL: -

http://www.somewhere.com/something.jsp#anchorValue

And from the Above URL we want to extract the text "anchorValue".

Now the browser will send only "http://www.somewhere.com/something.jsp" to the server and "anchorValue" will be used to find and internal Link in the HTML Document which is sent by the server as a part of response.

Refer this - Internal Links in a HTML

We cannot use Java to process "#" and its value.

Although JS can be used to find and manipulate the complete URL (including the text preceded by "#").

4 comments:

Javin @ Tibco RV Tutorial said...

Nice post dude , Good to know about this kind of issue. thanks for presenting solution also.

Javin
Why String is immutable in Java

Sumit said...

Thanks Javin

Anonymous said...

Can you help in understanding how can we achieve this with JS(with example would be greatly appreciated.

Sumit said...

By using "parent.document.URL" you can capture the everything displayed in the browser navigation bar.