Redirection and forwarding

Posted by kurky on Thu, 28 Oct 2021 12:42:36 +0200

1.1 redirection

1.1.1 concept of redirection

  • First, the client browser sends an http request. When the web server accepts it, it sends a 302 status code response and the corresponding new location to the client browser.
  • If the client browser finds that it is a 302 response, it will automatically send a new http request. The request url is the new location address. The server will find resources according to this request and send them to the client.

1.1.2 implementation of redirection

  • The following methods in the javax.servlet.http.HttpServletResponse interface are required to implement redirection:
Method declaration effect
void sendRedirect(String location) Sends a temporary redirect response to the client using the specified redirect location URL

1.1.3 principle of redirection

1.1.4 features of redirection

  • After redirection, the URL of the browser address bar will change.
  • During redirection, the previous Request object will be destroyed and a new Request object will be created.
  • The redirected URL can be another project.

1.1.5 code example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Redirect jump</title>
</head>
<body>
<form action="redirectServlet" method="post">
    <input type="submit" value="redirect">
</form>
</body>
</html>

Server

package cn.guardwhy.demo01;

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

@WebServlet("/redirectServlet")
public class RedirectServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Received a request from the browser");
        // 1. Redirect and send a new location to the browser
        response.sendRedirect(request.getContextPath() + "/test01.html");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

Page end

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Reset backward page</title>
</head>
<body>
    <h3>Page after server reassignment</h3>
</body>
</html>

results of enforcement

1.2 forwarding

1.2.1 concept of forwarding

A Web component (Servlet/JSP) transfers the unfinished processing to another Web component through the container for further processing and forwarding
All components of the share Request and Response objects.

1.2.2 implementation of forwarding

  • Bind data to Request object
Method declaration effect
Object getAttribute(String name) Returns the specified property value as an object. If the property with the given name does not exist, it returns a null value.
void setAttribute(String name,Object o) Store attribute values in this request.
  • Get forwarder object
Method declaration effect
RequestDispatcher getRequestDispatcher(String path) Returns a RequestDispatcher object that acts as a wrapper for resources located on a given path
  • Forwarding operation
Method declaration effect
void forward(ServletRequest request, ServletResponse response) Forward the request from one servlet to another resource on the server (servlet, JSP file, or HTML file)

1.2.3 forwarding characteristics

  • The URL of the browser address bar will not change after forwarding.
  • The Request object is shared during forwarding.
  • The forwarded URL cannot be another project.

1.2.4 principle of forwarding

1.2.5 code example

Front page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Forward jump</title>
</head>
<body>
<form action="forwardServlet01" method="post">
    <input type="submit" value="forward">
</form>
</body>
</html>

Server

package cn.guardwhy.demo02;

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

@WebServlet("/forwardServlet01")
public class ForwardServlet01 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Received a request from the browser");
        // 1. Add a key value pair to the request field
        request.setAttribute("username", "kobe");
        // 2. Forward, that is, let the Web component transfer the task to another Web component
        request.getRequestDispatcher("/forwardServlet02").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
package cn.guardwhy.demo02;

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

@WebServlet("/forwardServlet02")
public class ForwardServlet02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("The data is forwarded");
        // 1. Remove the user name from the request domain
        String username = (String) request.getAttribute("username");
        System.out.println("Get user name for:" + username);
        // 2. Set code
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("Remove the user from the request domain:" + username);
        out.write("<h3>Forwarding succeeded...</h3>");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

results of enforcement

1.2.6 difference between redirection and forwarding

difference forward() Redirect sendRedirect()
Address bar Will not change The new address is displayed
Where to jump Server side Browser side
Request data in domain Not lost Will be lost
root directory http://localhost:8080/ Project name http://localhost:8080

Topics: Java