Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher does not update URL in Address Bar

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, I got it. Thanks Bear.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic