JAVA-JSP内置对象之request范围

 

相关资料:
《21天学通Java Web开发》

 

request范围
1.在一次请求内有效。
2.如果页面从一个页面跳转到另一个页面,那么属性就失效了。
3.如果使用服务器端跳转<jsp:forward>,则属性仍然有效。
4.通过使用request的setAttribute()方法来设置属性,并通过request的getAttribute()。

requestdemo.jsp(用户跳转)

 1 <%@ page language="java" contentType="text/html;charset=gb2312" %>
 2 <html>
 3 <head>
 4   <title>request范围</title>
 5 </head>
 6 <body>
 7   <%--在request 范围设置属性--%>
 8   <%
 9     request.setAttribute("name","James");//设置属性name,其值为James
10   %>
11   <a href="requestdemo2.jsp">跳转到requestdemo2.jsp /a>
12 </body>
13 </html>
View Code

 

requestdamo2.jsp

 1 <%@ page language="java" contentType="text/html;charset=gb2312" %>
 2 <html>
 3 <head>
 4   <title>request范围</title>
 5 </head>
 6 <body>
 7   <%--取得request 范围属性--%>
 8   <%
 9     String strName=(String)request.getAttribute("name");//取值属性name的值
10     out.println=("request范围:name属性值为"+strName);//输出name属性值
11   %>
12 </body>
13 </html>
View Code

 

requestdamo3.jsp(服务器跳转)

 1 <%@ page language="java" contentType="text/html;charset=gb2312" %>
 2 <html>
 3 <head>
 4   <title>request范围</title>
 5 </head>
 6 <body>
 7   <%--在request范围设置属性--%>
 8   <%
 9     request.setAttribute("name","James");//设置属性name,其值为james
10   %>
11   <jsp:forward page="requestdemo2.jsp"></jsp:forward>
12 </body>
13 </html>
View Code

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值