forword和redirect的区别(The difference between forword and redirect)

 用户向服务器发送了一次HTTP请求,该请求可能会经过多个信息资源处理以后才返回给用户,各个信息资源使用请求转发机制相互转发请求,但是用户是感觉不到请求转发的。根据转发方式的不同,可以区分为直接请求转发(Forward)和间接请求转发(Redirect),那么这两种转发方式有何区别呢?本篇在回答该问题的同时全面的讲解两种请求转发方式的原理和区别。

【关键考点】

请求转发的含义;

Forward转发请求的原理;

Redirect转发请求的原理。

【考题分析】

  Forward和Redirect代表了两种请求转发方式:直接转发和间接转发。

  直接转发方式(Forward),客户端和浏览器只发出一次请求,Servlet、HTML、JSP或其它信息资源,由第二个信息资源响应该请求,在请求对象request中,保存的对象对于每个信息资源是共享的。

间接转发方式(Redirect)实际是两次HTTP请求,服务器端在响应第一次请求的时候,让浏览器再向另外一个URL发出请求,从而达到转发的目的。

举个通俗的例子:

 直接转发就相当于:“A找B借钱,B说没有,B去找C借,借到借不到都会把消息传递给A”;

  间接转发就相当于:”A找B借钱,B说没有,让A去找C借”。

下面详细阐述一下两者的原理:

一:间接请求转发(Redirect)

  间接转发方式,有时也叫重定向,它一般用于避免用户的非正常访问。例如:用户在没有登录的情况下访问后台资源,Servlet可以将该HTTP请求重定向到登录页面,让用户登录以后再访问。在Servlet中,通过调用response对象的SendRedirect()方法,告诉浏览器重定向访问指定的URL,示例代码如下:

……//Servlet中处理get请求的方法publicvoid doGet(HttpServletRequest request,HttpServletResponse response){//请求重定向到另外的资源response.sendRedirect(“资源的URL”);————————————————版权声明:本文为CSDN博主「CoderIsWeaver」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/chengquanlam/article/details/88400876

————————

The user sends an HTTP request to the server. The request may be returned to the user after being processed by multiple information resources. Each information resource forwards the request to each other using the request forwarding mechanism, but the user does not feel the request forwarding. According to different forwarding methods, it can be divided into direct request forwarding and indirect request forwarding. What is the difference between the two forwarding methods? While answering this question, this article comprehensively explains the principles and differences of the two request forwarding methods.

[key test points]

Meaning of request forwarding;

The principle of forward forwarding request;

The principle of redirect forwarding request.

[analysis of test questions]

Forward and redirect represent two request forwarding methods: direct forwarding and indirect forwarding.

In the direct forward mode, the client and browser only send a request once. The servlet, HTML, JSP or other information resources are responded to by the second information resource. In the request object request, the saved object is shared for each information resource.

Indirect forwarding is actually two HTTP requests. When the server responds to the first request, it asks the browser to send a request to another URL, so as to achieve the purpose of forwarding.

Take a popular example:

Direct forwarding is equivalent to: “a borrows money from B, B says no, B borrows money from C, and if it can’t be borrowed, it will pass the message to a”;

Indirect forwarding is equivalent to: “a asks B to borrow money, B says no, let a ask C to borrow”.

The principles of the two are described in detail below:

1: Indirect request forwarding (redirect)

Indirect forwarding, sometimes called redirection, is generally used to avoid abnormal access by users. For example, if a user accesses background resources without logging in, the servlet can redirect the HTTP request to the login page so that the user can access it after logging in. In the servlet, call the sendredirect() method of the response object to tell the browser to redirect access to the specified URL. The example code is as follows:

… / / public void doget (HttpServletRequest, httpservletresponse response) {/ / request redirection to another resource response.sendredirect (“resource URL”); — — — — — — — — — — — — — — — — — — — — — — — — The original article of follows CC 4.0 by-sa copyright agreement. For reprint, please attach the original source link and this statement. The original link: https://blog.csdn.net/chengquanlam/article/details/88400876