Difference between SendRedirect and forward is one of classical interview questions asked during java web developer interview. This is not just applicable for servlet but also for JSP in which we can use forward action or call sendRedirect() method from scriptlet. Before examining difference on forward and SendRedirect let’s see what send Redirect method and forward method does.
 
SendRedirect ():  
 
This method is declared in HttpServletResponse Interface.
 
Signature: void sendRedirect(String url)

Read more: http://javarevisited.blogspot.com/2011/09/sendredirect-forward-jsp-servlet.html#ixzz2xmpb4zvh

This method is used to redirect client request to some other location for further processing ,the new location is available on different server or different context.our web container handle this and transfer the request using  browser ,and this request is visible in browser as a new request. Some time this is also called as client side redirect.
 
 
Forward():
This method is declared in RequestDispatcher Interface.
Signature: forward(ServletRequest request, ServletResponse response)
 
This method is used to pass the request to another resource for further processing within the same server, another resource could be any servlet, jsp page any kind of file.This process is taken care by web container when we call forward method request is sent to another resource without the client being informed, which resource will handle the request it has been mention on requestDispatcher object which we can get by two ways either using ServletContext or Request. This is also called server side redirect.
 
 
RequestDispatcher rd = request.getRequestDispatcher("pathToResource");
  rd.forward(request, response);
 
Or
 
RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
  rd.forward(request, response);
 
 
 

Difference between SendRedirect and Forward

 
Now let’s see some difference between these two method of servlet API in tabular format.
 
Forward()
SendRediret()
When we use forward method request is transfer to other resource within the same server for further processing.
In case of sendRedirect request is transfer to another resource to different domain or different server for futher processing.
 
In case of forward Web container handle all process internally and client or browser is not involved.
 
When you use SendRedirect container transfers the request to client or browser so url given inside the sendRedirect method is visible as a new request to the client.
 
When forward is called on requestdispatherobject we pass request and response object so our old request object is present on new resource which is going to process our request
 
In case of SendRedirect call old request and response object is lost because it’s treated as new request by the browser.
Visually we are not able to see the forwarded address, its is transparent
In address bar we are able to see the new redirected address it’s not transparent.
 
Using forward () method is faster then send redirect.
SendRedirect is slower because one extra round trip is required beasue completely new request is created and old request object is lost.Two browser request requird.
 
When we redirect using forward and we want to use same data in new resource we can use request.setAttribute () as we have request object available. (That means you can not add any url parameter in the forward, you can not changed the original url in a way. So, if you want to pass any data to the next servlet or jsp. we can use setAttribute and getAttribute. Please remember forward is in the same context.)
But in sendRedirect if we want to use we have to store the data in session or pass along with the URL.
 
 
 

Example of forward and SendRedirect in JSP Servlet:

 
Any kind of online payment when we use merchant site will redirect us to net banking site which is completely new request it process our request and again redirect to merchant site?
 
In Banking Application when we do login normally we use forward method. In case of online banking we are asked for username and password if it’s a correct some another servlet or resource will handle the request other wise request has been forwarded to error page.
 

Which one is good?

 
Its depends upon the scenario that which method is more useful.
 
If you want control is transfer to new server or context and it is treated as completely new task then we go for Send Redirect.
Normally forward should be used if the operation can be safely repeated upon a browser reload of the web page will not affect the result.
 
SendRedirect and forward method are still very useful while programming or working on any web application project using servlet jsp. This is still a popular interview questions so don’t forget to revise forward and sendRedirect before appearing for any job interview.
 

Read more: http://javarevisited.blogspot.com/2011/09/sendredirect-forward-jsp-servlet.html#ixzz2xmpgi4Ie

see also: http://javarevisited.blogspot.com/2011/09/sendredirect-forward-jsp-servlet.html

difference between forward and sendredirect的更多相关文章

  1. jsp:forward response.sendRedirect

    jsp中<jsp:forward page=""/>和response.sendRedirect("")两种跳转的区别 一.response.sen ...

  2. forward和sendRedirect的区别

    部分内容转自:http://blog.csdn.net/zhouysh/article/details/380364和http://blog.csdn.net/frank_jay/article/de ...

  3. JSP的重定向有两种forward和sendRedirect

    jsp:forward重定向 当index.jsp存放在tomcat服务器应用目录下时:D:\Tomcat 7.0\webapps\Spring_shizhan4ban_Chapter05\index ...

  4. JSP页面跳转之sendRedirect()与forward()辨析

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6044817.html  在JSP中,要实现页面的跳转,主要有两种方式实现:forward和sendRedire ...

  5. java 中sendredirect()和forward()方法的区别

     一.文章1 HttpServletResponse.sendRedirect与RequestDispatcher.forward方法都可以实现获取相应URL资源. sendRedirect实现请求重 ...

  6. HttpServlet RequestDispatcher sendredirect和forward

    Servlet的框架是由两个Java包组成:javax.servlet和javax.servlet.http. 在javax.servlet包中定义了所有的Servlet类都必须实现或扩展的的通用接口 ...

  7. Forward reference vs. forward declaration

    Q:Im a bit confused. What is the difference between forward declaration and forward reference? Forwa ...

  8. JSP内置对象--response对象 (addCookie(),setHeader(),sendRedirect())

    服务器接收客户端请求:request 服务器对客户端的回应:response javax.servlet.http的接口HttpServletResponse extends ServletRespo ...

  9. 浅谈forword和sendRedirect

    最近项目中全部用ajax请求数据,导致在做登录过滤器时不能重定向,然后仔细翻了翻Forward和sendRedirect,以下内容收集自百度: 1. forward (服务器端作的重定向) 服务器往c ...

随机推荐

  1. 对HTML5校验 自定义验证信息

    在HTML5中,表单可以定义一个属性required来触发默认的校验机制,比如: <input type="text" required /> 弹出的错误提示默认为英文 ...

  2. IdentityServer4 简单使用,包括api访问控制,openid的授权登录,js访问

    写在前面 先分享一首数摇:http://music.163.com/m/song?id=36089751&userid=52749763 其次是:对于identityServer理解并不是特别 ...

  3. 前端常用的几个js判断(二)

    10.鼠标悬停(hover)切换 class 属性假如当用户鼠标悬停在一个可点击的元素上时,你希望改变其效果,下面这段代码可以在其悬停在元素上时添加 class 属性,当用户鼠标离开时,则自动取消该 ...

  4. $(window).height()获取到的高度不对

    关于这个问题,网上有人说这是webkit内核的浏览器导致的,且只有这种浏览器会出现,只需要将html5的声明整成标准的带有<!doctype>的声明的就行了,但是本人亲测仍然是不行的,所用 ...

  5. occ添加新的捕捉模式

    Load (theSelection, theShape, theType, theDeflection, theDeviationAngle, isAutoTriangulation, thePri ...

  6. tab切换-2016.6.4

    以前的tab切换,一般都是自己找网上的源代码,不知道含义,直接套,然后会有一些不知道的问题出现. 最近学习了jq(当然属于懒人的我,学习进度很慢),然后再工作中遇到了tab选项卡,所以决定自己写一个. ...

  7. Tomcat 启动项目报错 java.lang.OutOfMemoryError: Java heap space

    近日使用myeclipse 部署web项目,启动tomcat时报错: SEVERE: Error waiting for multi-thread deployment of directories ...

  8. 一个面试题的解答-----从500(Id不连续)道试题库里随机抽取20道题!

    做一个考试系统的项目,现在从试题库里面随机抽取20道题 比如我题库有500道题(ID不连续).题目出现了,如何解决呢,随机抽取! 1,我们先把500道题的id存进一个长度为500的数组. 2,实现代码 ...

  9. IHttpHandler防止图片链接被盗用

    public class JpegHandler : IHttpHandler { public bool IsReusable { get { return true; } } public voi ...

  10. Bootstrap <基础三十一>插件概览

    在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...