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

查看:381
本文介绍了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

then I get this url: http://localhost:8080/login.jsp

但如果我使用

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

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

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

有什么区别?

推荐答案

简单解释一下差异,

  response.sendRedirect("login.jsp");

不预先添加contextpath(指的是捆绑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。然后浏览器向给定网址发起新请求。

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天全站免登陆