RequestDispatcher.forward()和HttpServletResponse.sendRedirect()之间有什么区别? [英] What's the difference between RequestDispatcher.forward() and HttpServletResponse.sendRedirect()?

查看:241
本文介绍了RequestDispatcher.forward()和HttpServletResponse.sendRedirect()之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RequestDispatcher forward() HttpServletResponse sendRedirect()方法?

任何人都可以通过实例解释这些方法的示例和最佳用法吗?

What is the difference between RequestDispatcher's forward() and HttpServletResponse's sendRedirect() method?
Can anyone explain with a example and best usage of these methods with a real time example?

推荐答案

重定向是一种发送回客户端的响应,而正向委派完全在服务器端进行,正向操作的结果返回给客户端,好像它只来自原始URL。

Redirection is a type of response sent back to the client, whereas the forward delegation takes place completely on the server side and the result of the forward action returns to the client as if it came only from the original URL.

另一个区别是前向委派只能用于应用程序内资源,而重定向
命令可以将客户端浏览器重定向到当前域之外。

Another difference is that forward delegation can be used only for in-applications resources, whereas redirection command can redirect the client browser outside the current domain.

// Sends a temporary redirect to the HTTP client. Only absolute URLs are allowed.
ServletResponse.sendRedirect(String location);


// Delegates one HttpRequest to another dynamic or static resource
HttpRequest.getRequestDispatcher("example.jsp").forward(request, response);


// Includes/enriches current response with another dynamic or static resource
HttpRequest.getRequestDispatcher("example.html").include(request, response);



另一个很好的解释可以在这里找到:

sendRedirect()与forward()之间的区别

这篇关于RequestDispatcher.forward()和HttpServletResponse.sendRedirect()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆