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

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

(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. vim编辑器的简单使用

    写这篇文章是因为在更新我的一篇博客 Git的其他用法 的时候,里面的修改已经提交的commit说明这一部分需要用到vim. 在使用git config --global --edit或者git reb ...

  2. html5后台界面

    Binary Admin Save Binary Admin is 100% free for personal & commercial use under MIT license. Liv ...

  3. HTML5之Audio音频标签学习

    HTML5中的新元素标签 src:音频文件路径. autobuffer:设置是否在页面加载时自动缓冲音频. autoplay:设置音频是否自动播放. loop:设置音频是否要循环播放. control ...

  4. 删除style的样式JQuery

      有些页面样式不规范,没有写在一个class里,例如:<div id="show" style="width:100px; padding-top:10px; f ...

  5. (五)python的发展历史

    1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器.Python这个名字,来自Guido所挚爱的电视剧Monty Python's Flying Circus.他希望这个新的叫做 ...

  6. (四)python自带解释器(LDIE)的使用

    什么是IDE? Integrated Development Environment(集成开发环境) 打个不恰当的比方,如果说写代码是制作一件工艺品,那IDE就是机床.再打个不恰当的比方,PS就是图片 ...

  7. 十件你需要知道的事,关于openstack-trove(翻译)

    开源数据库即服务OpenStack Trove应该知道的10件事情 作者:Ken Rugg,Tesora首席执行官 Ken Rugg是Tesora的创始人,CEO和董事会成员. Ken的大部分职业都是 ...

  8. iOS 电商购物车倒计时时间计算

    /** * 倒计时 * * @param endTime 截止的时间戳 * * @return 返回的剩余时间 */ - (NSString*)remainingTimeMethodAction:(l ...

  9. 安卓---apk反编译

    转自:http://blog.csdn.net/vipzjyno1/article/details/21039349 在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮 ...

  10. linux之LVS简介(转自南非的蚂蚁)

    一. LVS简介LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,它的官方站点是www.linuxvirtualserver. ...