Forward requests

Request forwarding is a server behavior. When a request from the client arrives, the server forwards the request. At this time, the request object will be saved and the URL in the address bar will not change. The implementation method is as follows to achieve the effect of multiple resources responding together.

request.getRequestDispatcher(url).forward(request,response)
Copy the code
  • Features:
  1. The server can only jump to the current internal resource
  2. The browser address bar remains unchanged
  3. Forward only one request
  4. Request data can be shared

Let’s explain these features in code.

This is requestDemo2

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("/requestDemo2")
public class requestDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        System.out.println("requestDemo2-->"+username);
        request.getRequestDispatcher("/requestDemo3").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response); }}Copy the code

This is requestDemo3

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("/requestDemo3")
public class requestDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        response.getWriter().write("This is the page for requestDemo3");
        String username = request.getParameter("username");
        System.out.println("requestDemo3-->"+username);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response); }}Copy the code

Pass username=zhangsan to requestDemo2, then look at this image.The Server prints these two results, and we see that by accessing requestDemo2 we are also accessing the requestDemo3 page, but the browser address bar is unchanged.

In addition, it can be found through the browser packet capture that there is only one request from the beginning to the end in the forwarding process, and the server only initiates a request, but the result is that both resources are accessed, so there is only one request for forwarding.

If you havegetRequestDispatcher(url)Replace with Baidu’s website, the page jumps will occur 404 pages there is no error, because Baidu’s website is not the internal resources of the server.

The request scope

Through request objects, data can be transmitted in a request. After request forwarding, data in the request domain still exists, and data can be transmitted and shared through request.

// Set the domain object content
request.setAttribute(String name, String value)
// Get the domain object content
request.getAttribute(String name)
// Delete the domain object
request.removeAttribute(String name)
Copy the code

redirect

Redirection is server to guide the client behavior, the client sends out the first request, after the server receives the processing, the server will respond, in response, the server will give the client a new resource address (that is, the next request address), when the client to receive the response, according to the new address immediately launched a second request, The server receives the request and responds, and the redirect is complete.

/* The first method */
// Set the status code to 302
response.setStatus(302);
// Set the response header location
response.setHeader("location",url);
/* The second method */
response.sendRedirect(url)
Copy the code

Without further ado, this is requestDemo2

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("/requestDemo2")
public class requestDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        System.out.println("requestDemo2-->"+username);
        response.sendRedirect("/requestDemo3");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
Copy the code

This is requestDemo3

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("/requestDemo3")
public class requestDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        System.out.println("requestDemo3-->"+username);
        response.setContentType("text/html; charset=utf-8");
        response.getWriter().write("demo3....");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response); }}Copy the code



Pass username=zhangsan to requestDemo2, then look at this image.

The status code of requestDemo2 is 302, the location is /requestDemo3, and the address bar of the browser is changed.

RequestDemo3 prints zhangsan but requestDemo2 does not, so redirection cannot share data with Request because it is two requests. Redirects can also access resources on other servers.

conclusion

Request forwarding versus redirection

Request forward Redirect redirect
A request Two requests
Data is shared in the Request domain Data in the Request domain is not shared
The browser address bar does not change The browser address bar changed
Only resources under the current server can be accessed You can access resources from other servers
Server behavior Client behavior