response.sendRedirect() 和 request.getRequestDispatcher().forward(request,response) 有什么区别 [英] What is the difference between response.sendRedirect() and request.getRequestDispatcher().forward(request,response)

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

问题描述

我在使用JAVA时出现页面跳转问题,如果我使用:

I have got a problem with my page jump when I use JAVA, if I use:

response.sendRedirect("login.jsp")

然后我得到这个网址:http://localhost:8080/login.jsp

但是如果我使用

request.getRequestDispathcer("login.jsp").forward(request, response)

然后我得到这个网址:http://localhost:8080/Shopping/login.jsp(Shopping"是我的模块的名称).

then I get this url: http://localhost:8080/Shopping/login.jsp (the "Shopping" is the name of my module).

有什么区别?

推荐答案

为了简单说明区别,

  response.sendRedirect("login.jsp");

不预先设置上下文路径(指捆绑 servlet 的应用程序/模块)

doesn't prepend the contextpath (refers to the application/module in which the servlet is bundled)

但是,而

 request.getRequestDispathcer("login.jsp").forward(request, response);

将在相应应用程序的上下文路径前面

will prepend the contextpath of the respective application

此外,重定向请求用于将资源重定向到不同的服务器或域.这个控制权的转移任务由容器委托给浏览器.也就是说,重定向将标头发送回浏览器/客户端.此标头包含浏览器要重定向的资源 url.然后浏览器向给定的 url 发起一个新的请求.

Furthermore, Redirect request is used to redirect to resources to different servers or domains. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url.

转发请求用于转发到进行调用的服务器中可用的资源.这种控制转移是由容器内部完成的,不涉及浏览器/客户端.

Forward request is used to forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved.

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

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