Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

difrrenc between sendRidirect and forward  RSS feed

 
Sunil K Bansal
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
pls clear me on response.sendRedirect and
jsp forward tag.
in which situation which is more beneficial.
advantage and disadvantage of using either
thanks
Bansal
 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunil, this has been discussed soooo many times. Search this forum and the servlets forum.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bansal
Simply put, when you send a redirect, the server tells the browser to go to another resource. In a forward it is invisible to the client and the browser, the new resource is returned as if it had come from the originally requested one.
A redirect would be if the user tried to access something that they had to log in for , you could redirect them to a login page. While a forward would be if you need to examine the request to determine what to do with it then forward it to another resoure for processing. The client doesn't neccesarily need to know that you've done that, they just want the results.
hope that helps clear it up a little.
 
Sunil K Bansal
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried both resonse.sendRedirect() and jsp:forward tag.
I come to know that using sendRedirect() previous parameters are not passed to redirected page and the URL changes.
with JSP forward tag previous parameters are passed to the forwarded page but URL do not change.
I m using tomcat 4 but on actually when I applied
response.sendRidirect() i m getting very bad output. output from previous page and redirected page.
where is the problem
thanx and regards
Bansal
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunil you may need to use one of response method calls to clear the buffer before sending it to the browser in case of a redirect. The problem is that you have already written to the before before redirecting the response
Use one of
response.flushBuffer() or
response.resetBuffer() based on whether you want to reset the Headers or not.
 
Sunil K Bansal
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
is there any difference in JSP and Servlets.
response.sendReDirect() is working fine in Servlets
but giving me bad out put in JSPs
kindly explain me ....
thanx
Bansal
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in servlets you have to explicitly write output back to the browser using:
out.println("some stuff")
comments. In JSP, the following will return a return to the client:
<%@ page language="java"%>
<%
%>
why? well, despite the fact that there doesnt seem to be any HTML, the line return between the first line and the rest is enough to send the line return that seperates them. If you want to guarantee no content is returned and you wont learn how to use the buffers then you will need to rewrite your jsps to:
<%@ page language="java"%><%
%>
so that there is absolutely nothing the compiler could consider as requiring sending back to the browser.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!