<%@ page contentType="text/html" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body><% //设置page属性范围,此属性只在当前的JSP有用 pageContext.setAttribute("name","柳"); pageContext.setAttribute("birthday",new Date()); %><% String username =(String)pageContext.getAttribute("name"); Date userbirthday =(Date)pageContext.getAttribute("birthday");%><h2>姓名:<%=username %></h2><h2>生日:<%=userbirthday %></h2><% //设置request属性范围,此属性只在服务器跳转有用 request.setAttribute("name2","柳1"); request.setAttribute("birthday2",new Date()); %><% //设置session属性范围,此属性只在一个浏览器中始终有用 session.setAttribute("name3","柳111"); session.setAttribute("birthday3",new Date()); %><% //设置 application 属性范围,此属性保存在服务器上 application.setAttribute("name4","柳11111"); application.setAttribute("birthday4",new Date()); %><jsp:forward page="page_02.jsp"/> </body></html>
**************************************************************************************
<%@ page contentType="text/html" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> <head> <title>My JSP 'index.jsp' starting page222</title> </head> <body><%
String username =(String)pageContext.getAttribute("name"); Date userbirthday =(Date)pageContext.getAttribute("birthday");%><h2>姓名:<%=username %></h2><h2>生日:<%=userbirthday %></h2><% String username2 =(String)request.getAttribute("name2"); Date userbirthday2 =(Date)request.getAttribute("birthday2");%><h2>姓名:<%=username2 %></h2><h2>生日:<%=userbirthday2 %></h2><% String username3 =(String)session.getAttribute("name3"); Date userbirthday3 =(Date)session.getAttribute("birthday3");%><h2>姓名:<%=username3 %></h2><h2>生日:<%=userbirthday3 %></h2><% String username4 =(String)application.getAttribute("name4"); Date userbirthday4 =(Date)application.getAttribute("birthday4");%><h2>姓名:<%=username4 %></h2><h2>生日:<%=userbirthday4 %></h2> </body></html>
从javax.servlet.jsp.PageContext类中可以发现,可以指定page属性的范围
1. public static final int PAGE_SCOPE page属性范围,默认2. public static final int REQUEST_SCOPE request属性范围3. public static final int SESSION_SCOPE session属性范围4. public static final int APPLICATION_SCOPE application属性范围pageContext.setAttribute("name","chen",pageContext.REQUEST_SCOPE);
request 主要作用是接收客户端发送来的请求,request是javax.servlet.http.HttpServletRequest接口的实例化对象定义如下: public interface HttpServletRequest extends ServletRequest