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

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

(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. SVN使用教程之-分支/标记 合并 subeclipse

    首先说下为什么我们需要用到分支-合并.比如项目demo下有两个小组,svn下有一个trunk版.由于客户需求突然变化,导致项目需要做较大改动,此时项目组决定由小组1继续完成原来正进行到一半的工作[某个 ...

  2. 在egret中使用protobuf

    原文章删除,重新使用MarkDown排版 在H5游戏领域,对于服务端与客户端的通信协议有一个选择,那就是使用protobuf.js.对于那些直接使用JavaScript开发的引擎而言,protobuf ...

  3. 还在为小三角形切图?使用纯CSS写一个简单的三角形

    同学们,当美工给的设计图是这样: 或者这样: 我的内心其实是拒绝的-_-:但工作还得干,大部分同学会写 <style> .icon{width:20px;height:20px;displ ...

  4. suse linux11 包括所有的linux操作系统的 遗忘root密码解决方案

    2017-1-13号,用户要割接ocs系统应用,因为不能直接给root密码,但是操作过程中出现密码修改出错,再次登录系统仍然有问题.去机房熬了将近6个小时,试过单用户模式(但需要密码),试过光盘救援模 ...

  5. git(安装)配置

    安装: git安装程序下载:https://git-scm.com/. 配置: $ git config --global user.name "Your Name" $ git ...

  6. mysql添加mcafee 审计插件

    插件源码地址https://github.com/mcafee/mysql-audit插件安装方法https://github.com/mcafee/mysql-audit/wiki/Installa ...

  7. 自己通过centos6.5配置NFS 成功后的笔记,希望对需要的人有点点帮助吧!

         环境介绍:            服务器:centos  172.16.250.170            客户端:centos  172.16.250.172     先用rpm -qa ...

  8. Cracking the Coding Interview 第二章

    2.2 链表中倒数第k个结点 输入一个链表,输出该链表中倒数第k个结点. 思路:快慢指针(error: 判断是否有可行解,否则返回null, while, if 后加空格) /* public cla ...

  9. 关于Kafka使用IBM Java报错解决方案

    安装环境 Ubuntu 14.04 Java IBM Java 1.7.0_79 Kakfa 2.10-0.8.2.1 使用bin/kafka-server-start.sh config/serve ...

  10. js 中的 exec( )方法

    JavaScript exec() 方法 JavaScript RegExp 对象 定义和用法 exec() 方法用于检索字符串中的正则表达式的匹配. 语法:RegExpObject.exec(str ...