e hënë, 11 qershor 2007

sendRedirect() vs. forward()

Redirect sends an http header back to the browser which causes it to issue a new request for the url indicated in the redirect. This involves an extra round-trip between the server and the browser and you lose all the request parameters unless you have appended them to the redirect url. The reason you would want to use a redirect is in situations where if the user clicks the refresh button or a back button, you would be re-executing a servlet which should not be re-executed. The classic example is an order confirmation page. If the user clicks the refresh button, you don't want to place a duplicate order. To avoid duplicate orders, the order confirmation page should always have been redirected to rather than forwarded to. Since your jsp is doing nothing, it should just forward to the servlet (redirect would be a waste).

URL encoding is used to add the session id to the url if the user's browser does not support cookies. The encodeURL and encodeRedirectURL methods of the HttpServletResponse class contain logic to determine if encoding is necessary so it doesn't hurt to call them. If a user's browser does not support cookies and you do not encode urls, each request will result in a new session.

By the way, your code should be changed to :
<%
String path = request.getContextPath() +
"/servlet/FrontController?thePage=\"index\"";
response.sendRidrect(path);
%>

Since you are already in a scriptlet (i.e. between <% %>), you don't use <%=. It should look like regular java code. Also, index/"" looks like a syntax error. If you are trying to escape the quote, it should be \" not /".

1 koment:

javin paul tha...

send redirect is mostly a client side redirect while forward is server side redirect. here is some difference between sendRedirect and Forward in Java