How to get the url?

Hi!

I need to get the value after the # sign in the url in Java.

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

How can I get “anchorValue” in Java?

Thanks.

Try String.split.

Hi!

Are you saying getting the “something.jsp#anchorValue” first then use string split?

How do I get the “something.jsp#anchorValue” in the first place?

Thanks.

something like this: -

String str = “http://www.somewhere.com/something.jsp#anchorValue”;

String str1 = str.split (“#”);

System.out.println(str1[1]);

Hi!

I need to get it from the url. I have tried request.getContextPath(), request.getURI(), request.getURL()…etc. but with no success. It doesn’t give me the value after the #.

Please help. Thanks.

try request.getRequestURI()

Tried. Doesn’t give the value after the #.

if i understand correctly you are hitting the browser with this URL

if yes than where are you getting the value of request.getRequestURI()
And also can you try if getQueryString() returns “anchorValue”?

Yes, you are right. But getQueryString() returns a null.

Here is the problem: -

whenever you type the above URL in the browser, the browser searches for the exact path typed in the URL till the time it reaches the extension which it recognizes and as soon it encounters an executable extension it tries to execute that (in your case it it something.jsp).

After processing the executable extension the first character it recognize is the “?” and after that you can give any number of parameter.

Note: the URL doesn’t accepts any special characters (apart from “/”), for e.g. type the below URL

In this case you will still see the output of something.jsp on the Browser.

If you really need to execute your URL than either replace “#” with “?”

or

remove the .jsp from the recognized extensions and use “/” instead of “#” and create a servlet which interprets the request and forward it to desirable URL.

I debugged it further and found that my earlier explanation is not correct.

“#” and the content preceding “#”, 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)

e.g.

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

from the above URL browser will send only “[I]http://www.somewhere.com/something.jsp[/I]” 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

You cannot use browser and java to process “#” and its value.

Although JS can be used to find the complete URL including the value.