When to use request forwarding? And when should you use request redirection? The difference between request forwarding and request redirection, and use

What is request forwarding?

Request forwarding refers to the operation of jumping from one resource to another after the server receives the request.

Insert picture description here

Characteristics of request forwarding

  1. The browser address bar will not change
  2. They request once, and the server completes the forwarding operation
  3. Share the data in the Request field
  4. Can be forwarded to the WEB-INF directory
  5. Cannot access resources other than the project

for example:

  1. A company has multiple departments, among which department 1 (Servlet1) provides customers with services that it can provide, but there are some services that cannot be provided by itself and require services from other departments;
  2. So department 1 (Servlet1) stamps the customer’s document to prove that department 1 has provided the service, and then forwards it (request forwarding) to department 2 (Servlet2);
  3. Department 2 checks whether department 1 has been stamped, and if it is stamped, it will continue to provide services to customers...;
  4. Until all services are processed, the data that the customer needs is provided to the customer (that is, the data is forwarded to the client **[forward to a page]**);
package servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Servlet1 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //1.servlet1先获取请求参数,看看servlet1需要做的事
        String username = request.getParameter("username");
        System.out.println(username);
        
        //.....部门1提供自己的服务
        
        //2.servlet1设置一个参数证明servlet1转发的请求(部门1盖一个章)
        request.setAttribute("key1", "servlet1");

        //3.查询servlet2的路径,(部门1将其转交给部门1)
        RequestDispatcher dispatcher = request.getRequestDispatcher("/servlet2");
        
        //4.把请求和响应转发给servlet2
        dispatcher.forward(request, response);
    }
}
package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Servlet2 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //部门2查看客户需要的服务
        System.out.println("Servlet2查看参数: "+request.getParameter("username"));
        //部门2查看部门1有没有盖章,判断是否继续服务
        System.out.println("Servlet2查看是否有Servlet1的证明: "+request.getAttribute("key1"));
        //部门2处理完自己的服务,
        System.out.println("Servlet处理自己的业务");
    }
}
Insert picture description here


note

At this time, if we put Servlet2 under WEB_INF, then we cannot directly access servlet2;

But we can achieve access to servlet2 through request forwarding;

Thinking: The login function is similar to this request forwarding method. We cannot skip the login interface and directly access the system. All we can use this request forwarding method to achieve;

What is request redirection?

Request redirection: refers to the client sending a request, and then the server tells the client, I will give you an address, you go to a new address to visit (the previous address may have been abandoned, or the previous address [Servlet] can not meet the demand);

Insert picture description here


The first option

// 设置响应状态码 302 ,表示重定向,(已搬迁) 
resp.setStatus(302); 
// 设置响应头,说明 新的地址在哪里 
resp.setHeader("Location", "http://localhost:8080");

The second scheme (recommended)

resp.sendRedirect("http://localhost:8080");

Characteristics of request redirection

  1. The address bar of the browser will change
  2. The browser sent two requests, instead of calling the getRequestDispatcher("/a/b/c.html").forward(request, response) method of request; therefore, a total of two request objects were created
  3. Two request objects cannot share domain data
  4. Redirection cannot access the resources in WEB-INF, because both requests are initiated by the client, and the client cannot directly access the resources in WEB-INF
  5. Can access resources outside the project (for example, Baidu transferred, etc.)

When to use request redirection?

  1. Browsing will record the last requestallData, the client can re-initiate the last request by refreshing the page;
  2. Before request forwarding or request redirection, we will always process some business; at this time, if this business is to add, delete or modify the database or local file, we need to use request redirection;

why?
Because at this time, if we use request forwarding (the entire processing business process is just a request), the browser will record our request (operation database), once the client refreshes the page, it will execute the request (operation database), this A very serious bug, we want to avoid it, all we need to use request redirection;

  1. Because every time we operate the database, we will jump to the home page, or the page where the data is displayed, so at this time we use request redirection to jump to this page (actually jump to a Servlet to query the records in the database, this Servlet Then jump to the data display page);
  2. At this time, the last request recorded by the browser is the request to query the records in the database. At this time, when we refresh the page, it is only to query the data, not the addition, deletion or modification of the database;

Summary: After we modify the data in the database, we need to use request redirection to avoid the above problems;