This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Hash symbol #  problem in URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Hash symbol #  problem in URL" Watch "Hash symbol #  problem in URL" New topic

Hash symbol # problem in URL

Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
I have a JSP page which has a ahref=""

Problem: When I click on the ahref, everything from # is stripped off in the URL
  • Try 1: Encoded # with %23 - did not work
  • Try 2: Used Java URL encoder class to encode

  • Try 1 & 2 are not working. Is there are way around to make this work.
    Paul Clapham

    Joined: Oct 14, 2005
    Posts: 19728

    When you say "doesn't work", do you mean your JSP generates HTML that contains an anchor named "aaa" and the browser fails to scroll to it? Or did you expect something else to happen? If so, what?
    Bear Bibeault
    Author and ninkuma

    Joined: Jan 10, 2002
    Posts: 63232

    This has nothing to do with JSP, so it's been moved to the HTML forum (where a duplicate post has already been closed).

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Esther Kak
    Ranch Hand

    Joined: Oct 11, 2006
    Posts: 51
    In my JSP I hardcoded the URL for ahref in Try1 - Result is everything from # has been stripped off.

    In try2, I used URLEncoder to store the encoded value in a string object and assigned the value to ahref like this

    String learnURL = ""+URLEncoder.encode("#aaa");


    It encodes # to %23 but it is not going where it is supposed to.
    Bear Bibeault
    Author and ninkuma

    Joined: Jan 10, 2002
    Posts: 63232

    If you are trying to use the # as an anchor, then encoding the character will simply make it not work (since you've changed the expected character).

    Rather you need to find out what's going on with using it normally.

    So you are saying that when you have something like:

    and you click on the link, that the "xyz" is stripped?

    Are you sure? It works fine for me in all browsers that I tried.

    Have you verified that the syntax of the <a> tag is really what you think it is?
    [ February 05, 2008: Message edited by: Bear Bibeault ]
    Ben Souther

    Joined: Dec 11, 2004
    Posts: 13410

    This is a case where either the LiveHTTPHeaders or the FireBug plugin for Firefox comes in really handy.

    With it, you'll be able to tell if the browser, or the server is stripping your characters out of the querystring. You'll also be able to find out if a request to the server is even being made (if you're already on the target page, the browser never makes a request to the server, it just moves to the specified anchor).

    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Eric Pascarello

    Joined: Nov 08, 2001
    Posts: 15385
    Are you trying to read the hash part on the client or on the server?

    I agree. Here's the link:
    subject: Hash symbol # problem in URL
    It's not a secret anymore!