Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect displays URL, forward does not?" Watch "sendRedirect displays URL, forward does not?" New topic
Author

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
Rancher

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

Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect displays URL, forward does not?
 
Similar Threads
sendRedirect or RequestDispatcher forward - error
forward action
response.sendRedirect(URL) Vs. RequestDispatcher
URGENT!!!!
Problems with Servlets + CSS + forward