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

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

(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应用的资源;

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

javaWEB总结(14):请求的转发和重定向的更多相关文章

  1. javaweb之请求的转发和重定向

    1.什么是请求转发和请求重定向? 请求转发: xxServlet收到请求,然后直接转发给yyServlet,然后yyServlet返回给客户端.整个过程中,客户端发出一个请求,收到一个响应. 重定向: ...

  2. jsp:和属性相关的方法,请求的转发,重定向

    jsp中与属性相关的方法: 方法: void setAttribute(String name, Object o): 设置属性 Object getAttribute(String name):获取 ...

  3. 域对象的作用范围 & 请求的转发和重定向

    1. 和属性相关的方法: ①. 方法 void setAttribute(String name, Object o): 设置属性 Object getAttribute(String name): ...

  4. JavaWEB - 请求的转发和重定向

    JavaWEB - Servlet

  5. 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  6. servlet请求转发于重定向

    请求的转发与重定向是Servlet控制页面跳转的主要方法,在Web应用中使用非常广泛. 一. 请求的转发 Servlet接收到浏览器端请求后,进行一定的处理,先不进行响应,而是在服务器端内部" ...

  7. SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解

    转自:http://blog.51cto.com/983836259/1877188 2016-11-28 09:45:59   如题所示,在SpringMVC中可以使用forward和redirec ...

  8. javaweb之Servlet,http协议以及请求转发和重定向

    本文是作者原创,版权归作者所有.若要转载,请注明出处. 一直用的框架开发,快连Servlet都忘了,此文旨在帮自己和大家回忆一下Servlet主要知识点.话不多说开始吧 用idea构建Servlet项 ...

  9. Java 请求转发和重定向的区别以及JavaWeb三大作用域

    三大作用域以及转发和重定向 学习总结 1. 转发和重定向 转发 重定向 转发和重定向的区别: 什么时候用转发什么时候用重定向 三大作用域 作用域类型 作用域方法 如何选择作用域 总结 学习总结 1. ...

随机推荐

  1. Oracle全角和半角处理函数

    1.TO_MULTI_BYTE语法: TO_MULTI_BYTE(String) 功能: 计算所有单字节字符都替换为等价的多字节字符的String.该函数只有当数据库字符集同时包含多字节和单字节的字符 ...

  2. 一口一口吃掉Hexo(二)

    如果你想得到更好的阅读效果,请访问我的个人网站 ,版权所有,未经许可不得转载! 本次系列教程的第二篇文章我会介绍如何在本地安装Hexo,请注意我使用的Windows系统,如果你是Mac或者Ubuntu ...

  3. 前端工具 - 15个最佳的 JavaScript 表单验证库

    客户端验证在任何项目都是不够的,因为 JavaScript 可以直接忽略,人们可以提交请求到服务器. 然而这并不意味着客户端验证都没必要了,很多时候我们需要在用户提交到服务器之前给予提示.JavaSc ...

  4. 【摘录】使用实体框架、Dapper和Chain的仓储模式实现策略

    以下文章摘录来自InfoQ,是一篇不错的软问,大家细细的品味 关键要点: Dapper这类微ORM(Micro-ORM)虽然提供了最好的性能,但也需要去做最多的工作. 在无需复杂对象图时,Chain这 ...

  5. C#微信公众号开发--网页授权(oauth2.0)获取用户基本信息二

    前言 这一篇实现snsapi_userinfo,写这篇时其实我是有疑惑的,因为我并没有调试成功,但是我反复检查程序和思路是没有问题的,因为我使用的测试公众号,群里一个伙计说他之前调试时用的也是测试公众 ...

  6. Python 购物车----之用户部分

    知识点: 文件读,写操作,if 判断, for 循环 salary = input("输入你的工资:") bought_list = [] product_list = {} wi ...

  7. sublime text3 3103 激活码

    —– BEGIN LICENSE —–Michael BarnesSingle User LicenseEA7E-8213858A353C41 872A0D5C DF9B2950 AFF6F667C4 ...

  8. JavaScript(暂时弃坑...)

    简单数据类型:字符串型.布尔型.数值型 变量名可以包含数字.字母.下划线.$,但不能以数字开头,大小写敏感,不能是JavaScript关键字.避开保留字 //JavaScript保留字 break e ...

  9. Hololens文件读写

    unity 内勾选 RemovableStorage 选项或 Package.appxmanifest 勾选 可移动存储 选项 uwp app IO操作用 StreamReader 会提示没有权限,如 ...

  10. NGINX----源码阅读----init配置脚本

    /auto/init init脚本负责初始化各种目录环境变量值. 1.make文件.源文件.头文件.配置头文件路径变量初始化. NGX_MAKEFILE=$NGX_OBJS/Makefile NGX_ ...