转发和重定向简介及与之相关的(URL)参数(parameter)、属性(attribute)问题探讨
<div id="article_content" class="article_content clearfix">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-211130ba7a.css">
<div id="content_views" class="markdown_views prism-dracula">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<p></p><div class="toc"><h3><a name="t0"></a><a name="t0"></a>文章目录</h3><ul><li><a href="#1_1" rel="nofollow" target="_self">1.引子</a></li><li><a href="#2_3" rel="nofollow" target="_self">2.转发和重定向</a></li><ul><li><a href="#1_4" rel="nofollow" target="_self">(1).转发</a></li><li><a href="#2_10" rel="nofollow" target="_self">(2).重定向</a></li></ul><li><a href="#3_19" rel="nofollow" target="_self">3.简单示例</a></li><li><a href="#4_102" rel="nofollow" target="_self">4.总结</a></li></ul></div><p></p>
1.引子
转发和重定向是我们在做web项目中常用到的两个术语,有必要理清两者的区别和与之相关的参数、属性获取问题。
2.转发和重定向
(1).转发
转发是服务器行为,将当前请求(Request)和响应(Response)处理打包发送给目标服务(Servlet),这样下一个目标服务就能获取或操作上一个服务中的请求和响应。
客户浏览器发送http请求—>web服务器接受此请求—>调用内部的一个方法在容器内部完成请求处理和转发动作—>将目标资源 发送给客户
转发行为是同一次请求,其URL地址仍是以前的地址,不会发生变化,但页面内容却是新页面的东西了。转发只能是在同一应用中使用,不能跨应用转发请求,比如,京东页面的请求就不可能转发给淘宝的服务器。
(2).重定向
重定向就如其名字一样,是将页面定位到一个新位置。重定向客户端行为,是全新的请求,客户端不能保存当前的请求,在定位到新的页面或servlet服务时,上次的请求超出请求的作用范围了,那个请求即失效了。
客户浏览器发送http请求—>web服务器接受后发送302状态码响应及对应新的location给客户浏览器—>客户浏览器发现 是302响应,则自动再发送一个新的http请求,请求url是新的location地址—>服务器根据此请求寻找资源并发送给客户。
重定向至少是两次请求,重定向后URL地址是新的地址了,当然页面内容也更新为目标页面的内容了。重定向可以定位到任意页面,可以跨越不同的应用程序。比如,天猫的页面就可以重定向到淘宝的页面上。
参数(parameter)、属性(attribute)的区别,可以看我另一篇帖子---- -Servlet中的属性(attribute)和参数(parameter)的区别
3.简单示例
登录页html代码
<form action="loginServlet" method="post">
用户名:
<input type="text" name="username" /> <br/>
密 码:
<input type="password" name="pwd"/><br/>
<!-- 默认的submit,提交给loginServlet处理 -->
<input type="submit" value="提交至将转发的servlet"> <br/>
<!-- 一个表单内容可以提交给不同的servlet,而formaction属性值就对应提交的目标地址,
这里提交给loginServlet2进行请求处理 -->
<input type="submit" formaction="loginServlet2" value="提交至将重定向的servlet">
</form>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
要转发的servlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String name=request.getParameter("username");
String pwd=request.getParameter("pwd");
System.out.println("用户名:"+name+"\t密码:"+pwd);
//request域中设置一个属性
request.setAttribute("nowDate", new Date());
//转发
request.getRequestDispatcher("forward_page.jsp?url_param=test_url_param").forward(request, response);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
要重定向的servlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String name=request.getParameter("username");
String pwd=request.getParameter("pwd");
System.out.println("用户名:"+name+"\t密码:"+pwd);
//request域中设置一个属性
request.setAttribute("nowDate", new Date());
//重定向
response.sendRedirect("redirect_page.jsp?url_param=test_url_param");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
将转发的登录页面
转发页的html代码
<body>
<h3>这是一个转发后的页面</h3>
request中获取参数(用户名):<label>${param.username}</label> <br/>
request中获取参数(密码):<label>${param.pwd}</label> <br/>
request中获取的属性(‘nowDate’):<label>${nowDate}</label> <br/>
url中获取的一个参数:<label>${param.url_param}</label> <br/>
</body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
servlet转发到的页面
从上图可以看出,所有的参数和属性(包括url中拼接的参数)都可以正确地获取,并显示在页面上。但url地址还是"loginServlet",不是当前页面所对应的"forward_page.jsp"地址。
将重定向的登录页面
重定向页的HTML代码
<body>
<h3>这是一个重定向后的页面</h3>
request中获取参数(用户名):<label><c:out value="${param.username}" default="空"/></label> <br/>
request中获取参数(密码):<label><c:out value="${param.pwd}" default="空"/></label> <br/>
request中获取属性‘nowDate’:<label><c:out value="${nowDate}" default="空"/></label> <br/>
url中获取的一个参数:<label><c:out value="${param.url_param}" default="空"/></label> <br/>
</body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
servlet重定向到的页面
从上图可以看出,上次请求的所有属性、参数均不能获取到。另外url地址更新了,不再是servlet的映射地址了,而当前新页面的"redirect_page.jsp"地址。
除此之外,可以看到重定向的URL中拼接的键值对参数“url_param=test_url_param”在新页面还是能获取到的。
4.总结
1.重定向,是一次新的请求,不能获得上次请求中的参数、属性;转发,是同一次请求,之前所有的参数、属性在新的页面或servlet中都可见的,它们保存在同一个request域中。
2.转发操作,(即使转至新页面)其URL地址不会变化;而在重定向操作中,URL地址会更新变为重定向的目标地址。
3.若要页面内容、URL地址均更新,并且新页面还要获取上次请求的参数、属性,可以通过一种曲线救国的方式实现:在重定向的URL中以键值对的方式来拼接上次请求的参数、属性;并在新页面中通过EL表达式“${param.name}”获取。
</div><div data-report-view="{"mod":"1585297308_001","dest":"https://blog.youkuaiyun.com/Xiaowu_First/article/details/95956806","extend1":"pc","ab":"new"}"><div></div></div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
<div data-report-view="{"mod":"popu_387","dest":"https://blog.youkuaiyun.com/Xiaowu_First/article/details/95956806","extend1":"pc","ab":"new"}"></div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.youkuaiyun.com/Xiaowu_First">
<img src="https://profile.csdnimg.cn/3/9/9/3_xiaowu_first" class="avatar_pic" username="Xiaowu_First">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit "><a href="https://blog.youkuaiyun.com/Xiaowu_First" data-report-click="{"mod":"popu_379","ab":"new"}" target="_blank">蜀中孤鹰</a></span>
<!-- 等级,level -->
<img class="identity-icon" src="https://csdnimg.cn/identity/blog4.png"> </div>
<div class="text"><span>原创文章 79</span><span>获赞 18</span><span>访问量 1万+</span></div>
</div>
<div class="right-message">
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379","ab":"new"}">关注</a>
<a href="https://im.youkuaiyun.com/im/main.html?userName=Xiaowu_First" target="_blank" class="btn btn-sm bt-button personal-letter">私信
</a>
</div>
</div>
</div>
</article>
<div class="hide-article-box hide-article-pos text-center" style="display: none;">
<a class="btn-readmore" id="btn-readmore-zk" data-report-click="{"mod":"popu_376","dest":"https://blog.youkuaiyun.com/Xiaowu_First/article/details/95956806","strategy":"readmore","ab":"new"}">
展开阅读全文
<svg class="icon chevrondown" aria-hidden="true">
<use xlink:href="#csdnc-chevrondown"></use>
</svg>
</a>
</div>
-
点赞
-
评论
-
收藏
-
手机看
分享到微信朋友圈
x扫一扫,手机阅读
-
<a data-report-click="{"mod":"popu_830" "dest":"","ab":"new"}"><img src="https://csdnimg.cn/release/phoenix/template/new_img/rewardWhite.png" alt=""><span class="name">打赏</span></a> <div id="reward" class="reward-box"> <p class="rewad-title">打赏<span class="reward-close"><svg t="1567152543821" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10924" xmlns:xlink="http://www.w3.org/1999/xlink" width="12" height="12"><defs><style type="text/css"></style></defs><path d="M512 438.378667L806.506667 143.893333a52.032 52.032 0 1 1 73.6 73.621334L585.621333 512l294.485334 294.485333a52.074667 52.074667 0 0 1-73.6 73.642667L512 585.621333 217.514667 880.128a52.053333 52.053333 0 1 1-73.621334-73.642667L438.378667 512 143.893333 217.514667a52.053333 52.053333 0 1 1 73.621334-73.621334L512 438.378667z" fill="" p-id="10925"></path></svg></span></p> <dl> <dd><a href="javascript:;"><img src="https://profile.csdnimg.cn/3/9/9/3_xiaowu_first" alt=""></a></dd> <dt> <p class="blog-name">蜀中孤鹰</p> <p class="blog-discript">“你的鼓励将是我创作的最大动力”</p> </dt> </dl> <div class="money-box"> <span class="choosed choose_money" data-id="5">5C币</span> <span class="choose_money" data-id="10">10C币</span> <span class="choose_money" data-id="20">20C币</span> <span class="choose_money" data-id="50">50C币</span> <span class="choose_money" data-id="100">100C币</span> <span class="choose_money" data-id="200">200C币</span> </div> <div class="sure-box"> <p class="is-have-money"><a class="reward-sure">确定</a></p> </div>
</li>
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<img src="https://csdnimg.cn/release/phoenix/template/new_img/lookMore.png" alt="">
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
<div style="display:flex">
<a class="tool-mover-open" id="btn-readmore" style="" data-type="0" data-report-click="{"mod":"popu_376","dest":"https://blog.youkuaiyun.com/Xiaowu_First/article/details/95956806","strategy":"packupbar","ab":"new"}" height="2">
<span>收起全文</span>
<img id="btn-close-img" style="display: block;" src="https://csdnimg.cn/release/phoenix/template/new_img/CommentArrow.png" alt="">
</a>
</div>
</div>


03-23 2万+