aspose file tools*
The moose likes Servlets and the fly likes RequestDispatcher does not update URL in Address Bar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher does not update URL in Address Bar" Watch "RequestDispatcher does not update URL in Address Bar" New topic
Author

RequestDispatcher does not update URL in Address Bar

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

I am fowarding the user from a servlet to a JSP page using
getServletContext().getRequestDispatcher(refURL).forward(request, response);
and when I do this, the URL in the address bar still shows the Servlet URL and not the JSP url. I know the foward is working correctly, but I just don't know why the URL isn't updating in the address bar.
Thanks.


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

That's the intended behavior. When you forward the request using the dispatcher (a completely server-side operation), you are remaining within the context of the same request which was initiated by the URL seen in the address bar. The browser has no knowledge that you are forwarding things around on the server, and so the address remains unchanged.
This is quite different from a redirect which asks the browser to initiate the new request. In this case, the browser is completely aware of what is going on and updates the address field accordingly.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

So I should be doing a response.sendRedirect() instead, right? Because I don't care about the request,response at this point. I just need to get back to the referring page.
[ September 18, 2003: Message edited by: Gregg Bolinger ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

Right, if you want your JSP to display in a different request than the servlet, use a redirect. If you want the JSP to execute in the same request (done very often in Model 2 since the servlet does the processing and sets data on the request for the JSP to display), use a forward.
hth,
bear
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Great, I got it. Thanks Bear.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: RequestDispatcher does not update URL in Address Bar