Hi..!

I am trying to get whole browser in my jsp page.
I have a sample url as, "http://www.ab.com/#/pages/ab"
I need this whole URL to be print in my scriptlet code in jsp.

I can't use javascript as that is disabled. I tried with REQUEST objets several methods like getURL and all but still not able to get whole URL as it displays in browser.

If you have a answer then can you please reply with snippet of code please.

Thank you in advance.

Dani AI

Generated

Short answer: you can’t reliably detect a literal # on the server because everything after # is a URL fragment (client‑side only) and browsers do not include the fragment in the HTTP request. That’s why sees only the base URL on the server, and why methods like the ones mentioned won’t show the #. The fragment is consumed by the browser (or client‑side router like Angular’s hash routes) and never reaches the servlet.

Practical options (pick one that fits your control over the client):

  • If you can use JavaScript (the usual fix): read location.hash and send it to the server as a query parameter or POST body. Example client-side pattern:

    if (location.hash) {
    window.location = '/saveFragment?frag=' + encodeURIComponent(location.hash.substring(1));
    }

    The server then reads the frag parameter.

  • If JS is not available (no client-side code at all): you must change how links are built so the needed information is sent to the server. Two approaches work:

    1. Encode the # as %23 when creating links (e.g. http://site.com/%23/...) — the server will see %23 and you can URL‑decode it to recover #.
    2. Avoid fragments for server‑needed data: move that data into the path or query string (for example http://site.com/pages/efg/hij.jsp or ?route=/pages/efg/hij.jsp) so the browser includes it in the request.

A tiny server-side note: if you decide to detect encoded hashes, inspect the raw URI/query (and URL‑decode it) rather than expecting a literal #. Relying on the Referer header for fragments is unreliable and not recommended.

Recommendation: the simplest robust fix is to change client links so the server receives the needed information (query/path or an encoded value). If you can re-enable even minimal JavaScript, capture location.hash and send it to the server — that’s the common, practical solution.

Recommended Answers

All 4 Replies

Hi..!

I am trying to get whole browser in my jsp page.
I have a sample url as, "http://www.ab.com/#/pages/ab"
I need this whole URL to be print in my scriptlet code in jsp.

I can't use javascript as that is disabled. I tried with REQUEST objets several methods like getURL and all but still not able to get whole URL as it displays in browser.

If you have a answer then can you please reply with snippet of code please.

Thank you in advance.

Strange I've never encountered getURL method in the HttpServletRequest's javadocs, Try searching here.

Hi

I am writing a jsp/servlet which can check that the requested URL is having "#" or not.?

Let's say my URL is like, http://abc.com/#/efg/hij.jsp

As it is having "#' sign in the URL string, So I need to check the existence of "#" in requested URL.

My problem is when I write ..

StringBuffer url = request.getRequestURL();
String requestedURL = url.toString();

requestedURL only provides me the string before "#" i.e. http://abc.com/
I need to check if it has "#" or not with whole URL string.

Is there any way to check that into jsp/servlet.?

see, i am not a JSP Developer so i don't know about it, but as a php developer i can suggest you to find some fuction like strpos() in php which is used to find the position occurrence of a given string.

I already gave you the link to the Javadocs, and from what you mention I think you need the getRequestURI() method and not the getRequestURL() method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.