1. 请求转发与请求重定向

    ①区别:

    • 本质区别:请求转发只发出一次请求,请求重定向则发出两次请求。
    • 请求转发:地址栏是初次发出请求的地址在最终的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)—请求转发和请求重定向的更多相关文章

  1. 请求转发 和 URL 重定向

    五 请求转发 和 URL 重定向 1 请求转发和重定向 干什么用? 是我们在java后台servlet中 由一个servlet跳转到 另一个 servlet/jsp 要使用的技术 前端发送请求到后台 ...

  2. JSTL、请求转发和URL重定向

    JSTL 为什么要使用JSTL? 因为在JSP中写JAVA代码很麻烦,而JSTL可以简化在JSp中写JAva代码的流程 如何使用JSTL? 准备工作: ①将JSTL依赖的jar包导入工程的WEB-IN ...

  3. Java Web中请求转发和请求包含

    1.都是在一个请求中跨越多个Servlet 2.多个Servlet在一个请求中,他们共享request对象.就是在AServle中setAttribute()保存数据在BServlet中由getAtt ...

  4. 20160326 javaweb 请求转发和请求包含

    (1)请求转发: this.getServletContext().getRequestDispatcher("").forward(request,response); requ ...

  5. getRequestDispatcher 中请求转发和请求包含的使用说明

    getRequestDispatcher() getRequestDispatcher() 包含两个方法,分别是请求转发和请求包含. RequestDispatcher rd = request.ge ...

  6. javaWeb中request请求转发和response重定向

    1.访问资源 运用forward方法只能重定向到同一个Web应用程序中的一个资源. 而sendRedirect方法可以让你重定向到任何URL.  2.request.get Forward代码中的&q ...

  7. 请求转发(forward)和重定向(redirect)的区别

    转发不会改变地址栏,重定向会. 转发是请求一次,重定向请求两次. 转发过程中只有一个request对象产生,重定向是两个. 转发不能转发到站外,重定向可以发送到站外. 重定向的第2个请求的请求方式是什 ...

  8. 请求转发和URL重定向的原理和区别

    一.请求转发和重定向是在java后台servlet中,由一个servlet跳转到另一个servlet/jsp要使用的技术 使用方法 请求转发  req.getResquestDispatcher(se ...

  9. java请求转发,响应重定向的区别

    请求转发:request.getRequestDispatcher().forward(); 例:request.getRequestDispatcher("/index.jsp" ...

随机推荐

  1. 剑指offer错题记录

    错误重点: 1. 传递vector参数时,如果调用函数改变了vector的内容,一定一定要&,传引用保持一致 旋转数组的最小数字:有重复数字情况,二分查找照样搞.情况考虑要周全,当a[mid] ...

  2. ionic 3 build后图片无法显示

    运行命令 ionic cordova build android 生成了android-debug.apk. /home/han/project/zero_app/platforms/android/ ...

  3. Json常用组件

    Json2.js   开发者:json官网:http://www.json.org/. 适用环境:用于在不支持JSON对象的浏览器(通常是国内使用IE内核的第三方浏览器)下使用.json2.js提供了 ...

  4. 大数据-kafka

    1Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 作用:1发布和订阅消息流,这个功能类似于消息队列,这也是kafka归类为消息队列框架的原因 2以容错 ...

  5. ip 命令

    Linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  6. rc.local(ubuntu18.04)

    系统自带服务/lib/systemd/system/rc-local.service 软连接为 /lib/systemd/system/rc.local.service -> rc-local. ...

  7. HUE的自动化安装部署

    HUE=Hadoop User Experience(Hadoop用户体验),直白来说就一个开源的Apache Hadoop UI系统,由Cloudera Desktop演化而来,最后Cloudera ...

  8. Python3 序列解包

    转载自:https://blog.csdn.net/yilovexing/article/details/80576788 序列解包是 Python 3.0 之后才有的语法 什么是序列解包呢?先看一个 ...

  9. hdu1598 find the most comfortable road (枚举)+【并查集】

    <题目链接> 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在 ...

  10. ecplise打不开提示Eclipse中...No java virtual machine was found...

    解决办法: 在eclipse.ini文件中最前面添加这两行: -vm C:\Program Files\Java\jdk1.8.0_191\bin\javaw.exe 上面那个路径是你的java jd ...