javaWEB总结(14):请求的转发和重定向


通俗理解请求转发与重定向的流程

通俗的来讲:我们可以把请求转发和重定向当成是一种借钱的情景模式。


(1)请求的转发:A向B借钱,B自己没有钱,但是向C借到了钱,并且把钱借给了A。A只向B请求了一次。
(2)请求的重定向:A向B借钱,B没有钱,A又向C借钱,C将钱借给了A。A向B请求了一次,又向C请求了一次,一共两次请求。

请求转发与重定向的具体过程

请求转发

(1)调用HttpServletRequest的getRequestDispather()方法获取请求转发器对象,调用getRequestDispather()需要传入要转发的地址。

(2)调用HttpServletRequest的forward(request,response)方法,进行请求的转发。


请求重定向

直接HttpServletResponse的sendRedirect方法并传入参数。

请求转发与重定向的java代码实现

请求转发

                //要转发的地址
		String path = "/forward.jsp";
		//转发
		request.getRequestDispatcher(path).forward(request, response);

请求重定向

                //要重定向的地址
		String path = "/redirect.jsp";
		//重定向
		response.sendRedirect(path);


下面是一个demo


web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>javaWeb_14</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


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>index</title>
</head>
<body>
<br><br>

<a href="forwardServlet?name=666">to forward.jsp</a>

<br><br>

<a href="redirectServlet?name=666">to redirect.jsp</a>

</body>
</html>


ForwardServlet.java


package com.dao.chu;

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;

/**
 * Servlet implementation class ForwardServlet
 */
@WebServlet("/forwardServlet")
public class ForwardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForwardServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("ForwardServlet");
		
		String name = request.getParameter("name");
		
		System.out.println("name is :"+name);
		
		//要转发的地址
		String path = "/forward.jsp";
		//转发
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}

RedirectServlet.java

package com.dao.chu;

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;

/**
 * Servlet implementation class RedirectServlet
 */
@WebServlet("/redirectServlet")
public class RedirectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RedirectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("RedirectServlet");
		
		String name = request.getParameter("name");
		
		System.out.println("name is :"+name);
		
		//要重定向的地址
		String path = "/redirect.jsp";
		//重定向
		response.sendRedirect(path);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}


forward.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>forward.jsp</title>
</head>
<body>

	<h2>forward.jsp</h2>
	<%
		String name = request.getParameter("name");
	
		if(null!=name&&!"".equals(name)){
			
			out.print("name is:"+name);
		}
	
	
	%>
</body>
</html>


redirect.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>redirect.jsp</title>
</head>
<body>

	<h2>redirect.jsp</h2>
	
	<%
		String name = request.getParameter("name");
	
		if(null!=name&&!"".equals(name)){
			
			out.print("name is:"+name);
		}
	
	%>
</body>
</html>


运行结果


主界面


点击to forward.jsp


点击to redirect.jsp


控制台


结论

(1)转发到forward.jsp页面的为同一个请求,而重定向到redirect.jsp页面的不是同一个请求;

(2)两次的地址栏不同。


请求转发与重定向区别

(1)转发发送一次请求,而重定向发送两次请求。(本质区别)

(2)请求转发:地址栏是初次发送请求的地址;

重定向:地址栏为最后响应的地址。

(3)请求转发:在最终的servlet中,request对象和中转的那个request为同一个对象;

重定向:在最终的servlet中,request对象和中转的那个request不是同一个对象;

(4)请求转发:只能转发给当前WEB应用的资源;

重定向:可以重定向到任何资源;







posted on 2017-01-08 13:32  不以物喜,不已己悲  阅读(243)  评论(0编辑  收藏  举报

导航