It's not a secret anymore!
The moose likes Servlets and the fly likes how to get current page's URL with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to get current page Watch "how to get current page New topic

how to get current page's URL with "doPost" method ?

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
I have a servlet which forwards to a JSP page. In the JSP page, I need to change <base href=".."> for few times because on my page there are links to other server domains. Meanwhile, in the page I create some anchors for sections in the page. Thus, if the anchor click needs to work, it needs to turn the href back to the current page's URL. However, the URL is always "http://server/servlet/MyServlet" because it uses POST method. I tried

<base href="http://server/servlet/MyServlet?"<%= request.getQueryString() %>">

but it doesn't work. what can I do to pull the <base> back to current page so that anchor for sections will work ?
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Why aren't you using relative URLs?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
Originally posted by Ben Souther:
Why aren't you using relative URLs?

For the anchor link I did use relative link. The problem is I need to turn the "base" into current page so that the relative link will work. And that's why I want to kow how I can do that using "request" related API.
John Wolf
Ranch Hand

Joined: Feb 18, 2002
Posts: 64

If you need a static variable, just create a new parameter called "current" and use requestDispatcher to forward it to the JSP.

In your JSP, use the following call: -

If you want to get the context than I would suggest using: -

This will give you "/" in your case.

Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

I am not sure I understand your problem completely.
But if the problem is that you are using a RequestDispatcher to forward the reponse to a JSP, and hence the url in address bar is still pointing to the original servlet, you can try using HttpServletResponse.sendRedirect() method instead.

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
he URL is always "http://server/servlet/MyServlet"

Your use of /servlet/ indicates that you may be getting tangled up in problems related to the "invoker" servlet. See this FAQ on the invoke here at the ranch.
I agree. Here's the link:
subject: how to get current page's URL with "doPost" method ?
It's not a secret anymore!