SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like this: -

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

    String str1[] = str.split ("#");

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

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try request.getRequestURI()

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried. Doesn't give the value after the #.

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if i understand correctly you are hitting the browser with this URL

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

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

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you are right. But getQueryString() returns a null.

  10. #10
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the problem: -

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

    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

    http://www.somewhere.com/something.jsp#pz#/xyz.jsp

    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.

  11. #11
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 "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

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

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •