File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes sendRedirect displays URL, forward does not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect displays URL, forward does not?" Watch "sendRedirect displays URL, forward does not?" New topic

sendRedirect displays URL, forward does not?

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
In my servlet, I've noticed that using the RequestDispatcher to forward to a JSP will not result in the JSP's path being displayed in the address bar of the web browser (it only displays the name of the servlet I POSTed to). When I use response.sendRedirect instead, it does change it to the path of the JSP.

Unless I'm mistaken, it's best to use forward in most cases because sendRedirect takes more time, right? Also, I guess it's often preferable to not reveal the underlying technology to the user (and hence you wouldn't want them to know it's a JSP). However, it seems that in some cases just leaving the servlet name up there could be confusing, too, and some semi-savvy users might try to mess with that by typing .html on the end or something else.

Maybe all this is trivial and it's really kind of arbitrary, or at least depends on what you need, but I'm putting this out there because I'm really wondering if there are best practices when you're posting to a servlet and what you should present to the user in the end (e.g. in the address bar).
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Realistically the two should not be interchangable. You'd use forward when you needed forward and sendRedirect when you need a redirect. It's not quite the same question, but it's similar to the comments I made here

I agree. Here's the link:
subject: sendRedirect displays URL, forward does not?
jQuery in Action, 3rd edition