转发是服务器行为,重定向是客户端行为。转发耗时比重定向少。
转发——>客户浏览器发送HTTP请求——>web服务器接受请求——>调用内部一个方法在容器内部完成请求处理和转发动作——>再将转发跳转到的那个网页资源返回给客户; 转发只能在同一个容器内完成 转发的时候浏览器地址是不会变的,在客户浏览器里只会显示第一次进入的那个网址或者路径,客户看不到这个过程,只是得到了想要的目标资源。转发行为浏览器只做了一次请求。(转发只能跳转一次)
重定向——>客户浏览器发送HTTP请求——>web服务器接受请求后发送302状态码以及新的位置给客户浏览器——>客户浏览器发现是302响应,则自动再发送一个新的HTTP请求,请求指向新的地址(302:Found 临时移动,但资源只是临时被移动。即你访问网址A,但是网址A因为服务器端的拦截器或者其他后端代码处理的原因,会被重定向到网址B。)——>服务器根据此请求寻找资源发个客户;再客户浏览器中显示的是重定向之后的路径,客户可以看到地址的变化。重定向行为浏览器做了至少两次请求。(重定向可以跳转多次)
从图解可以看出转发时客户端只发送了一次请求,而重定向时客户端需要发送至少两次请求。
如生活实例(假如去办个证件):
重定向:你先去了A局,A局的人说:“这个事情不归我们管,去B局”,然后,你就从A退了出来,自己乘车去了B局。
转发:你先去了A局,A局看了以后,知道这个事情其实应该B局来管,但是他没有把你退回来,而是让你坐一会儿,自己到后面办公室联系了B的人,让他们办好后,送了过来。
一、转发:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><form action="second" method="get"><input type="submit" value="登陆 "></form>
</body>
</html>
Second.java
package com.xianyadong.demo;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/second")
public class Second extends HttpServlet{private static final long serialVersionUID = 1L;public Second() {super();}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{System.out.println("=====");request.getRequestDispatcher("/welcome").forward(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{doGet(request, response);}}
Welcome.java
package com.xianyadong.demo;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/welcome")
public class Welcome extends HttpServlet {private static final long serialVersionUID = 1L;public Welcome() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");PrintWriter writer = response.getWriter();writer.println("<html>");writer.println("<body>欢迎"+"</body>");writer.println("</html>");}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
浏览器点击登录去访问Second.java 然后Second通过转发返回给浏览器Welcome.java的内容。
从图可以看出网站地址只会显示second不会出现welcome。
二、重定向
index.java 还用上面原来的
Second.java
package com.kaoshi.chongdingxiang;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/second")
public class Second extends HttpServlet {private static final long serialVersionUID = 1L;public Second() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.sendRedirect("welcome");}}
Welcome.java
package com.kaoshi.chongdingxiang;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/welcome")
public class Welcome extends HttpServlet {private static final long serialVersionUID = 1L;public Welcome() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");PrintWriter writer = response.getWriter();StringBuffer requestURL = request.getRequestURL();writer.println(requestURL.toString());writer.println("<br>");int status = response.getStatus();writer.println(status);writer.println("<html>");writer.println("<body>欢迎"+"</body>");writer.println("</html>");}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
从图片中网址那栏不难看出转发的后面是Second而重定义的后面则是Welcome。