scope1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <jsp:useBean id="pagevar" scope="page" class="java.lang.StringBuffer"/> <jsp:useBean id="requestvar" scope="request" class="java.lang.StringBuffer"/> <jsp:useBean id="sessionvar" scope="session" class="java.lang.StringBuffer"/> <jsp:useBean id="appvar" scope="application" class="java.lang.StringBuffer"/> <!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>演示作用域</title> </head> <body> <% pagevar.append("page1"); requestvar.append("page1"); sessionvar.append("page1"); appvar.append("page1"); %> <!-- 请求转发 --> <jsp:forward page="scope2.jsp"/> </body> </html>
scope2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <jsp:useBean id="pagevar" scope="page" class="java.lang.StringBuffer"/> <jsp:useBean id="requestvar" scope="request" class="java.lang.StringBuffer"/> <jsp:useBean id="sessionvar" scope="session" class="java.lang.StringBuffer"/> <jsp:useBean id="appvar" scope="application" class="java.lang.StringBuffer"/> <!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>演示作用域</title> </head> <body> <h3>操作结果:</h3> <hr> <% pagevar.append("page2"); requestvar.append("page2"); sessionvar.append("page2"); appvar.append("page2"); %> <pre> page = <%=pagevar.toString()%> request = <%=requestvar.toString()%> session = <%=sessionvar.toString()%> app = <%=appvar.toString()%> </pre> </body> </html>
下面来做测试
首先访问scope1.jsp
page = page2 [新页面]
request = page1page2 [同一请求]
session = page1page2 [同一会话]
app = page1page2 [同一应用]
随后把浏览器地址该成scope2.jsp
page = page2 [新页面]
request = page2 [新请求]
session = page1page2page2 [同一会话]
app = page1page2page2 [同一应用]
接着打开一新浏览器窗口访问scope2.jsp
page = page2 [新页面]
request = page2 [新请求]
session = page2 [新会话]
app = page1page2page2page2 [同一应用]
最后不关闭浏览器重新启动服务器,再刷新浏览器
page = page2 [新页面]
request = page2 [新请求]
session = page2page2 [同一会话]
app = page2 [新应用]