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应用的资源;
重定向:可以重定向到任何资源;
javaWEB总结(14):请求的转发和重定向的更多相关文章
- javaweb之请求的转发和重定向
1.什么是请求转发和请求重定向? 请求转发: xxServlet收到请求,然后直接转发给yyServlet,然后yyServlet返回给客户端.整个过程中,客户端发出一个请求,收到一个响应. 重定向: ...
- jsp:和属性相关的方法,请求的转发,重定向
jsp中与属性相关的方法: 方法: void setAttribute(String name, Object o): 设置属性 Object getAttribute(String name):获取 ...
- 域对象的作用范围 & 请求的转发和重定向
1. 和属性相关的方法: ①. 方法 void setAttribute(String name, Object o): 设置属性 Object getAttribute(String name): ...
- JavaWEB - 请求的转发和重定向
JavaWEB - Servlet
- 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- servlet请求转发于重定向
请求的转发与重定向是Servlet控制页面跳转的主要方法,在Web应用中使用非常广泛. 一. 请求的转发 Servlet接收到浏览器端请求后,进行一定的处理,先不进行响应,而是在服务器端内部" ...
- SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解
转自:http://blog.51cto.com/983836259/1877188 2016-11-28 09:45:59 如题所示,在SpringMVC中可以使用forward和redirec ...
- javaweb之Servlet,http协议以及请求转发和重定向
本文是作者原创,版权归作者所有.若要转载,请注明出处. 一直用的框架开发,快连Servlet都忘了,此文旨在帮自己和大家回忆一下Servlet主要知识点.话不多说开始吧 用idea构建Servlet项 ...
- Java 请求转发和重定向的区别以及JavaWeb三大作用域
三大作用域以及转发和重定向 学习总结 1. 转发和重定向 转发 重定向 转发和重定向的区别: 什么时候用转发什么时候用重定向 三大作用域 作用域类型 作用域方法 如何选择作用域 总结 学习总结 1. ...
随机推荐
- 【JS学习笔记】提取行间事件
行间提取事件第一种方法: function 名字() { ... } oBtn.onclick=名字: 第二种方法: oBtn.onclick=function () { ... } 其实在JS当中, ...
- H5与CS3权威下.18 and 19 选择器(1)
18章.CSS3概述 1.从前端技术的角度把互联网的发展分为三个阶段: (1)web1.0:HTML和CSS. (2)web2.0:Ajax,Javascript/DOM/异步数据请求. (3)web ...
- 如何得到AdoConnection.execute(sqlstr)执行的返回结果
如何得到AdoConnection.execute(sqlstr)执行的返回结果? 1: TAdoConnection.execute有procedure.function的两种定义,function ...
- package com.sun.image.codec.jpeg does not exist
rt.jar 是sun公司内部使用的包,不建议外部使用,rt.jar是被用了的,但是里面的com.sun下面的包不被默认加载了, JAVA7之前是默认加载的,所有用JAVA7以前的JDK编译是通过的. ...
- Extjs控件属性
Ext.form.TimeField: 配置项: maxValue:列表中允许的最大时间 maxText:当时间大于最大值时的错误提示信息 ...
- Ubuntu 16.04 Django安装和配置
之前有安装和配置过,换了台电脑,再安装和配置,忽然发现差不多都忘记了,这里记录下已备之后查阅. sudo apt-get install python-pip sudo apt-get install ...
- centos7 install rvm
不管其他,先按要求更新一下包 yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel li ...
- vedio_note2
例化 for example ex_cnt ex_cnt_inst( //模块名 例化的名字 .clk(ckl_o), // . 后面是原模块的名字 括号里面是top里的名字 .rst_n(rst_n ...
- python1 tkinter
turtle 图形: turtle.color(c) 设置笔的颜色turtle.fillcolor(c) 设置笔填充颜色turtle.begin_fill() 在填充颜色前访问这个方法turtle ...
- Jenkins slave image
Add a new shell script configure_slave.sh as following: #!/bin/bash dnf -openjdk git wget openssh-se ...