File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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: 15299
    
    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: 60782
    
  65

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: 15299
    
    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: 60782
    
  65

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: 15299
    
    6

Great, I got it. Thanks Bear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher does not update URL in Address Bar
 
Similar Threads
RequestDispatcher display the servlet URL, not the jsp
web.xml error-page throws exception on s:text
url problem
Request dispatcher
Use of forward and sendRedirect