This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why redirect? is forwarding not enough?  RSS feed

 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why redirect? is forwarding not enough?
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, forwarding may not be enough. First of all, you can't forward outside of the current web application. Also you may not want the user to see "http://www.server.com/login" when they have already logged in and are viewing the main page of the site.

-Yuriy
 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree with your first point. thanks.

Also you may not want the user to see "http://www.server.com/login" when they have already logged in and are viewing the main page of the site.


this can also be done with forward! in fact more effienctly !!
[ August 19, 2005: Message edited by: Arul Jose ]
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? If the servlet mapped to "/login" forwards to "/main", how do you get "http://..../main" to show up in the browser's location bar?

-Yuriy
 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did not imagine you were talking abt cross-application in the second point also. I thought after logging in you will be wandering in the same application.

Thanks.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I was talking about two different servlets in the same application. If the first servlet forwards to the second servlet, the URL for the first servlet will be the one displayed in the browser's location bar. As far as I know, the only way to get the URL for the second servlet to be displayed is to redirect instead of forwarding.

Also, if you forward a request, and the user does a refresh, the refresh request will be made to the original servlet, with all of the parameters. So if LoginServlet forwards to MainServlet and the user refreshes the page, the request will go back to LoginServlet with the login parameters. Furthermore the user will get that annoying "This page contains POST attributes, do you really want to resend again?" popup.

But if the LoginServlet redirects to MainServlet, a refresh will be a GET on the MainServlet, and the only parameters will be whatever parameters had been specified in the redirect - username and password don't get sent again.

-Yuriy
 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea. i get different views abt re-direct.


But if the LoginServlet redirects to MainServlet, a refresh will be a GET on the MainServlet, and the only parameters will be whatever parameters had been specified in the redirect - username and password don't get sent again.


i think using forward also you can prevent resending username and password.

so finally this can be done using forward also. right? only thing is you will get the 'annoying' message, that too only when you refresh.

I guess, sometimes the application may want to hide the url change on the address bar.
 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arul Jose:
why redirect? is forwarding not enough?


additional reason: forward cannot forward to one external to your container, while redirect can
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They're two different tools, with different properties.
In a nutshell:
forward = server side (no new request is made. the browser has no idea it has happened)
sendRedirect = client side (a 302 header is sent to the client suggesting that it make a new "GET" request to the specified resource)

Knowing the properties of the tools available and knowing when one is more apprpriate than another is the responsibility of the developer.
 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow. thanks. im happy.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!