JavaWeb-JSP 请求转发和请求重定向

JSP 请求转发和请求重定向


请求转发:
1.request是同一个请求

请求重定向:
1.request不是同一个请求,是两个


写个例子测试一下:
a.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>Insert title here</title>
</head>
<body>

    <a href="b.jsp">TO B PAGE</a>



</body>
</html>

b.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>Insert title here</title>
</head>
<body>

    <%
        request.getRequestDispatcher("/c.jsp").forward(request, response);
    %>

</body>
</html>

c.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>Insert title here</title>
</head>
<body>
    <h1>C JSP</h1>
</body>
</html>

运行:
这里写图片描述
点击链接
这里写图片描述
我们在b.jsp里面写的是请求转发:

<%
    request.getRequestDispatcher("/c.jsp").forward(request, response);
%>

说明请求转发是一个请求,打开谷歌浏览器的开发者工具即可:
这里写图片描述
我们再来测试请求重定向:
b.jsp

<%
    //request.getRequestDispatcher("/c.jsp").forward(request, response);
    // 重定向
    response.sendRedirect("c.jsp");
%>

运行效果:
这里写图片描述

可见地址改变, 打开开发者工具:
这里写图片描述
可见是两个请求。


再次写一个JSP内置属性来测试一下,先看四个属性的作用范围:
1.request -> 只限于同一个请求(用于测试是否是同一个请求)
2.pageContext -> 只限于本 JSP 页面
3.session -> 只限于同一次会话(浏览器关闭~重新启动浏览器 在此期间不会失效)
4.application -> 作用范围最大 整个WEB请求,(只要服务器没有关闭就不会失效)


att1.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>Insert title here</title>
</head>
<body>
    <h1>att1 JSP</h1>
    <%
        request.setAttribute("requestAttr", "requestValue");
        session.setAttribute("sessionAttr", "sessionValue");
        pageContext.setAttribute("pageContextAttr", "pageContextAttrValue");
        application.setAttribute("applicationAttr", "applicationValue");
    %>
    <br>
    <br> pageContext:
    <%=pageContext.getAttribute("pageContextAttr")%>
    <br>
    <br> request:
    <%=request.getAttribute("requestAttr")%>
    <br>
    <br> session:
    <%=session.getAttribute("sessionAttr")%>
    <br>
    <br> application:
    <%=application.getAttribute("applicationAttr")%>

    <br>
    <a href="att2.jsp">att2 JSP</a>

</body>
</html>

att2.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>Insert title here</title>
</head>
<body>
    <h1>att2 JSP</h1>
    <br>
    <br> pageContext:
    <%=pageContext.getAttribute("pageContextAttr")%>
    <br>
    <br> request:
    <%=request.getAttribute("requestAttr")%>
    <br>
    <br> session:
    <%=session.getAttribute("sessionAttr")%>
    <br>
    <br> application:
    <%=application.getAttribute("applicationAttr")%>

</body>
</html>

运行效果:
这里写图片描述
点击链接
这里写图片描述
由于不是同一个请求,所以第二个页面request的值为null
我们改一下代码进行测试:
1 -> 请求转发
att1.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>Insert title here</title>
</head>
<body>
    <h1>att1 JSP</h1>
    <%
        request.setAttribute("requestAttr", "requestValue");
        session.setAttribute("sessionAttr", "sessionValue");
        pageContext.setAttribute("pageContextAttr", "pageContextAttrValue");
        application.setAttribute("applicationAttr", "applicationValue");
    %>
    <br>
    <br> pageContext:
    <%=pageContext.getAttribute("pageContextAttr")%>
    <br>
    <br> request:
    <%=request.getAttribute("requestAttr")%>
    <br>
    <br> session:
    <%=session.getAttribute("sessionAttr")%>
    <br>
    <br> application:
    <%=application.getAttribute("applicationAttr")%>

    <%
        // 请求转发
        request.getRequestDispatcher("/att2.jsp").forward(request, response);

        //response.sendRedirect("c.jsp");
    %>

</body>
</html>

测试效果图:
这里写图片描述
地址不变 而且request的值获取到了 可见是请求转发是同一个请求
我们再来测试一下重定向:
att1.jsp

<%
    // 请求转发
    //request.getRequestDispatcher("/att2.jsp").forward(request, response);

    // 请求重定向
    response.sendRedirect("att2.jsp");
%>

测试效果:
这里写图片描述
可见地址改变 值为null 所以是不同的两个请求


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值