首页 > 代码库 > jsp_属性范围_request

jsp_属性范围_request

request属性范围表示在服务器跳转后,所有设置的内容依然会被保留下来。(服务器端跳转:页面跳转,地址栏不发生变化)

下面写个小例子测试下:

(1)request_demo.jsp

 1 <%@ page contentType="text/html; charset=utf-8" language="java"     errorPage="" %>
 2 <%@ page import="java.util.*"%>
 3 <!doctype html>
 4 <html>
 5 <head>
 6 <meta charset="utf-8">
 7 <title>page属性范围</title>
 8 </head>
 9 
10 <body>
11     <%
12         request.setAttribute("name","ninic");
13         request.setAttribute("birthday",new Date());
14     %>
15     <jsp:forward  page="request_demo2.jsp"/>
16 </body>
17 </html>

(2)request_demo2.jsp

 1 <%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*" errorPage="" %>
 2 <!doctype html>
 3 <html>
 4 <head>
 5 <meta charset="utf-8">
 6 <title>无标题文档</title>
 7 </head>
 8 
 9 <body>
10      <%
11         String username=(String)request.getAttribute("name");
12         Date userbirthday=(Date)request.getAttribute("birthday");
13     %>
14     <h2>姓名:<%=username%></h2>
15     <h2>生日:<%=userbirthday%></h2>
16 </body>
17 </html>

浏览器中显示:

技术分享

jsp_属性范围_request