web development (eight) forwarding, forwarding and redirection of difference

Achieve Forward:

  • The method of obtaining the request object repeater RequestDispatcher getRequestDispatcher ( "forwarding address") return value is the transponder
  • The method of using the subject repeater forward (request, response)

Graphic:

Here Insert Picture Description

Code:

package com.ccc.demo01Dispatcher;

import javax.servlet.RequestDispatcher;
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(urlPatterns = "/servlet1")
public class Servlet1 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Servlet1 我是刘备");

        //1.获取转发器RequestDispatcher
        RequestDispatcher dispatcher = request.getRequestDispatcher("/servlet2");
        
        //2.调用转发器RequestDispatcher中方法forward实现转发
        dispatcher.forward(request,response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
package com.ccc.demo01Dispatcher;

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(urlPatterns = "/servlet2")
public class Servlet2 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Servlet2 我是关羽");
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("二哥借你500");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

Forwarding and redirection difference:

Redirect:

  1. Requested twice server, the address bar changes . Servlet1 from the beginning -> becomes the sevlet2
  2. Redirection can be directed to the external network
  3. Redirection address (if it is outside the network) must have to write the name of the project . / day12_web / servlet2
  4. Redirect the final result: the impact of the data in the servlet2

Forward:

  1. He requested a server, without any change in the address bar . servlet1 -> or sevlet1
  2. Forwards not to the external network . Forwarding behavior just inside the server, the client does not know this behavior
  3. Forwarding path not need to write the name of the project . / servlet2. Name because it is within the current project, so the project can be omitted
  4. Data is also forwarded servlet2 final results, the impact of the

When a forward, when to use redirection

  1. External network when needed, can only redirect
  2. General address bar of the browser you want to make changes, then use the redirect (page if Jingdong, the login is successful, jump to the home page, this time it should become a home address of address)
  3. Generally do not want to address changes, use forward (if Jingdong page, login fails, the login page also allows users to continue to log in), once the request is valid request object
    Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_45083975/article/details/92650152