Servlet(10)—请求转发和请求重定向
请求转发与请求重定向
①区别:- 本质区别:请求转发只发出一次请求,请求重定向则发出两次请求。
- 请求转发:地址栏是初次发出请求的地址在最终的Servlet中,request对象和中转的那个request是同一个对象
只能转发给当前web应用的资源
/表示当前web应用的根目录 - 请求重定向:地址栏不再是初次发出的请求地址,地址栏为最后响应的那个地址在最终的Servlet中,request对象和中转的那个request不是同一个对象可以重定向到任何资源
/表示当前web站点的根目录
②源代码
- 请求的转发:
public class ForWardServlet extends HttpServlet {
private static final long serialVersionUID = 1l;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
System.out.println("ForWordServvlet's doGet");
req.setAttribute("name", "abcd");
System.out.println("ForWordServlet:" + req.getAttribute("name"));
//请求的转发
//调用HttpServletRequest的getRequestDispatcher(path)方法获取RequestDispatcher对象,
//调用getRequestDispatcher(path)需要填入转发的地址
String path = "TestServlet";
RequestDispatcher requestDispatcher = req.getRequestDispatcher(path);
//2.调用HttpSerlvetReuqest的forward(request, response)进行请求转发
requestDispatcher.forward(req, resp);
}
}
- 请求的重定向
public class RedirectServlet extends HttpServlet {
private static final long serialVersionUID = 1l;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("RedirectServlet's doGet");
req.setAttribute("name", "efgh");
System.out.println("RedirectSerlvet's name:" + req.getAttribute("name"));
//请求执行重定向,直接调用response的sendRedirect(path)方法,其中path为重定向的地址
String path = "testServlet";
resp.sendRedirect(path);
}
}
2 . 请求转发与重定向和对象、属性一块使用(实例)
a.jsp
<body>
<h1>A page</h1>
<!-- 方式一:include指令 -->
<%@ include file="b_1.jsp" %>
<!-- 方式二:jsp标签 -->
<jsp:include page="b_2.jsp">
<jsp:param value="1234" name="username"/>
</jsp:include>
<!-- 其作用与请求转发一致 -->
<%-- <jsp:forward page="b_3.jsp">
<jsp:param value="123" name="username"/>
</jsp:forward> --%>
<!-- 乱码问题 -->
<form action="Attr_1.jsp" method="get">
<input type="text" name="username" value="中文">
<input type="submit" value="Submit">
</form>
<%
//设置属性
pageContext.setAttribute("pageContextAttr", "pageContextValue");
request.setAttribute("requestAttr", "requestValue");
session.setAttribute("sessionAttr", "sessionValue");
application.setAttribute("applicationAttr", "applicationValue");
%>
</body>
①Attr_1.jsp:
<h1>Attr_1 page</h1>
<!-- 获取属性 -->
pageContextAttr:<%= pageContext.getAttribute("pageContextAttr") %>
<br><br>
requestAttr:<%= request.getAttribute("requestAttr") %>
<br><br>
session:<%= session.getAttribute("sessionAttr") %>
<br><br>
application:<%= application.getAttribute("applicationAttr") %>
<br><br>
<a href="Attr_2_1.jsp">To Attr_2_1 Page</a>:请求转发到Attr_3.jsp
<br><br>
<a href="Attr_2_2.jsp">To Attr_2_2 Page</a>:请求重定向到Attr_3.jsp
<br><br>
<a href="Attr_2_3.jsp">To Attr_2_3 Page</a>:请求转发向到Attr_3.jsp,以获取request对象的属性值
<br><br><br><br>
<!-- 处理post请求的处理方式 -->
<%
request.setCharacterEncoding("UTF-8");
%>
username(处理post):<%= request.getParameter("username") %>
<br><br><br>
<!-- 乱码问题 -->
<!-- 处理get请求处理方式 -->
<%
String val = request.getParameter("username");
String username = new String(val.getBytes("UTF-8"), "UTF-8");
out.println("username(处理get方法一):" + username);
%>
<!-- 这样处理get请求非常麻烦,可以在apache中server.xml中和eclipse中Servlet项目中的servlet.xml文件相同的位置
设置usebodyEncoding的值 -->
<br><br><br>
username(处理get方法二):<%= request.getParameter("username") %>
②Attr_2_1.jsp:
<h1>Attr_2_1 page</h1>
<%
//请求转发
request.getRequestDispatcher("/Attr_3.jsp").forward(request, response);
%>
③Attr_2_2.jsp:
<h1>Attr_2_2 page</h1>
<%
//请求重定向
response.sendRedirect("Attr_3.jsp");
%>
④Attr_2_3.jsp:
<h1>Attr_2_3 page</h1>
<%
//设置属性
pageContext.setAttribute("pageContextAttr", "pageContextValue");
request.setAttribute("requestAttr", "requestValue");
session.setAttribute("sessionAttr", "sessionValue");
application.setAttribute("applicationAttr", "applicationValue");
%>
<%
//请求转发
request.getRequestDispatcher("/Attr_3.jsp").forward(request, response);
%>
<br>
⑤Attr_3.jsp:
<h1>Attr_3 page</h1>
<!-- 获取属性 -->
pageContextAttr:<%= pageContext.getAttribute("pageContextAttr") %>
<br><br>
requestAttr:<%= request.getAttribute("requestAttr") %>
<br><br>
session:<%= session.getAttribute("sessionAttr") %>
<br><br>
application:<%= application.getAttribute("applicationAttr") %>
⑥页面访问:
请求的页面 到达的页面
——> Attr2_1.jsp ——>请求转发, ——> Attr_3.jsp 转发到Attr_3.jsp之后的地址http://localhost:8080/Demo5/Attr_2_1.jsp
Attr_1.jsp ——> Attr2_2.jsp ——>请求重定向, ——> Attr_3.jsp 到Attr_3.jsp之后的地址http://localhost:8080/Demo5/Attr_3.jsp
——> Attr2_3.jsp ——>请求转发 ——> Attr_3.jsp (request对象获取requestAttr属性值,以证实request的属性范围),转发后的
地址http://localhost:8080/Demo5/Attr_2_3.jsp
③Attr_2_1.jsp
<body>
<h1>Attr_2_1 page</h1>
<%
//请求转发
request.getRequestDispatcher("/Attr_3.jsp").forward(request, response);
%>
</body>
④Attr_2_2.jsp
<body>
<h1>Attr_2_2 page</h1>
<%
//请求重定向
response.sendRedirect("Attr_3.jsp");
%>
</body>
⑤Attr_2_3.jsp
<body>
<h1>Attr_2_3 page</h1>
<%
//设置属性
pageContext.setAttribute("pageContextAttr", "pageContextValue");
request.setAttribute("requestAttr", "requestValue");
session.setAttribute("sessionAttr", "sessionValue");
application.setAttribute("applicationAttr", "applicationValue");
%>
<%
//请求转发
request.getRequestDispatcher("/Attr_3.jsp").forward(request, response);
%>
<br>
</body>
⑥Attr_3.jsp
<body>
<h1>Attr_3 page</h1>
<!-- 获取属性 -->
pageContextAttr:<%= pageContext.getAttribute("pageContextAttr") %>
<br><br>
requestAttr:<%= request.getAttribute("requestAttr") %>
<br><br>
session:<%= session.getAttribute("sessionAttr") %>
<br><br>
application:<%= application.getAttribute("applicationAttr") %>
</body>
结果:
Attr_1.jsp中三个超链接的结果是
超链接1:
超链接2:
超链接3:
Servlet(10)—请求转发和请求重定向的更多相关文章
- 请求转发 和 URL 重定向
五 请求转发 和 URL 重定向 1 请求转发和重定向 干什么用? 是我们在java后台servlet中 由一个servlet跳转到 另一个 servlet/jsp 要使用的技术 前端发送请求到后台 ...
- JSTL、请求转发和URL重定向
JSTL 为什么要使用JSTL? 因为在JSP中写JAVA代码很麻烦,而JSTL可以简化在JSp中写JAva代码的流程 如何使用JSTL? 准备工作: ①将JSTL依赖的jar包导入工程的WEB-IN ...
- Java Web中请求转发和请求包含
1.都是在一个请求中跨越多个Servlet 2.多个Servlet在一个请求中,他们共享request对象.就是在AServle中setAttribute()保存数据在BServlet中由getAtt ...
- 20160326 javaweb 请求转发和请求包含
(1)请求转发: this.getServletContext().getRequestDispatcher("").forward(request,response); requ ...
- getRequestDispatcher 中请求转发和请求包含的使用说明
getRequestDispatcher() getRequestDispatcher() 包含两个方法,分别是请求转发和请求包含. RequestDispatcher rd = request.ge ...
- javaWeb中request请求转发和response重定向
1.访问资源 运用forward方法只能重定向到同一个Web应用程序中的一个资源. 而sendRedirect方法可以让你重定向到任何URL. 2.request.get Forward代码中的&q ...
- 请求转发(forward)和重定向(redirect)的区别
转发不会改变地址栏,重定向会. 转发是请求一次,重定向请求两次. 转发过程中只有一个request对象产生,重定向是两个. 转发不能转发到站外,重定向可以发送到站外. 重定向的第2个请求的请求方式是什 ...
- 请求转发和URL重定向的原理和区别
一.请求转发和重定向是在java后台servlet中,由一个servlet跳转到另一个servlet/jsp要使用的技术 使用方法 请求转发 req.getResquestDispatcher(se ...
- java请求转发,响应重定向的区别
请求转发:request.getRequestDispatcher().forward(); 例:request.getRequestDispatcher("/index.jsp" ...
随机推荐
- x86 版的 Arduino Intel Galileo 开发板的体验、分析和应用
1.前言 在今年(2013)罗马举办的首届欧洲 Make Faire 上,Intel 向对外发布了采用 x86 构架的 Arduino 开发板:Intel Galileo.这无疑是一个开源硬件领域的重 ...
- 【Android】android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding="ut ...
- 使用spark集成kudu做DDL
spark对kudu表的创建 定义kudu的表需要分成5个步骤: 1:提供表名 2:提供schema 3:提供主键 4:定义重要选项:例如:定义分区的schema 5:调用create Table a ...
- kafka其中一台节点坏掉的迁移或者数据迁移
kafka版本:适用于目前2.0以下 第一步: 假如有一个topic叫做test,当前topic的详情是这样的: [cdh@cdh1 kafka_2.11-1.0.1]$ bin/kafka-topi ...
- P1074 靶形数独 dfs回溯法
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...
- 05. Matplotlib 1 |图表基本元素| 样式参数| 刻度 注释| 子图
1.Matplotlib简介及图表窗口 Matplotlib → 一个python版的matlab绘图接口,以2D为主,支持python.numpy.pandas基本数据结构,运营高效且有较丰富的图表 ...
- 富文本编辑器上传图片需要配置js,后台代码
富文本编辑器上传图片需要配置js,后台代码
- 在Visual Sutdio 2017中使用boost库
在Visual Sutdio 2017中使用boost库 转载 https://blog.csdn.net/u011054333/article/details/78648294 对C++有一 ...
- HDU 2389 Rain on your Parade 最大匹配(模板题)【HK算法】
<题目链接> 题目大意:有m个宾客,n把雨伞,预计时间t后将会下大雨,告诉你每个宾客的位置和速度,每把雨伞的位置,问你最多几个宾客能够拿到伞. 解题分析: 本题就是要我们求人与伞之间的最大 ...
- MyEclipse、Eclipse使用配置及部分问题
简单总结一下myeclipse首次使用的配置,eclipse类似.总结的不是很全面,如有新的看法,欢迎下方留言. 最优设置 1.myeclipse激活 myeclipse.eclipse程序及激活工具 ...