通过底层源代码深度剖析请求转发和重定向的区别

文章目录

  1. HttpServletResponse 对象的 sendRedirect(String location)方法称作重定向。如果 location 地址前面加上“/”,则表示相对于 Servlet容器的根来请求,即 http://localhost:8080,如果 location 地址前没有加上“/”,则表示相对于当前请求的 URI 来寻找地址。
<%@ 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="MyForwardServlet" method="post">
username: <input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>
package learn.servlet;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MyForwardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String username = request.getParameter("username");
		
		request.setAttribute("username", username);
		
//		response.sendRedirect("myResult.jsp");
		
		
        RequestDispatcher dispatcher = request.getRequestDispatcher("myResult.jsp");
		
		dispatcher.forward(request, response);
		
		/*String username = request.getParameter("username");
		
		request.setAttribute("username", username);
		
		List list = new ArrayList();
		
		for(int i = 0; i < 100; i++){
			list.add(String.valueOf(i));
		}
		
		request.setAttribute("list", list);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("myResult.jsp");
		
		dispatcher.forward(request, response);*/
	}


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

}

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'myResult.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   
  username:<%=request.getAttribute("username") %>
      
  </body>
</html>

重定向结果为:
通过底层源代码深度剖析请求转发和重定向的区别_第1张图片
转发:
通过底层源代码深度剖析请求转发和重定向的区别_第2张图片

  1. RequestDispatcher 的 forward(request, response)方法称作请求转发。
  2. 请求转发与重定向的区别。
    1)请求转发,整个过程处于同一个请求当中
    通过底层源代码深度剖析请求转发和重定向的区别_第3张图片
    2)重定向。实际上客户端会向服务器端发送两个请求。
    通过底层源代码深度剖析请求转发和重定向的区别_第4张图片
    3 ) RequestDispatcher 是 通 过 调 用 HttpServletRequest 对象的getRequestDispatcher()方法得到的,是属于请求对象的方法。
    4)sendRedirect()是 HttpServletResponse 对象的方法,即响应对象的方法,既然调用了响应对象的方法,那就表明整个请求过程已经结束了,服务器开始向客户端返回执行的结果。

你可能感兴趣的:(Java,Web,JSP)