标签:属性 技术分享 tle 保存 ext.get 示例 odi size doctype
1.什么是jsp的属性范围?
所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以保存并继续使用。jsp提供了四种属性范围,如下:
2.属性的操作方法
public void setAttribute(String name,Object value) 设置属性
public object getAttribute(String name) 取得属性
public void removeAttribute(String name) 删除属性
示例代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘attrTest1.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% pageContext.setAttribute("pageContextAttr", "pageContextValue"); request.setAttribute("requestAttr", "requestValue"); session.setAttribute("sessionAttr", "sessionValue"); application.setAttribute("applicationAttr", "applicationValue"); %> <br><br> pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %> <br><br> request: <%=request.getAttribute("requestAttr") %> <br><br> session: <%=session.getAttribute("sessionAttr") %> <br><br> application: <%=application.getAttribute("applicationAttr") %> </body> </html>
运行后输出结果如下:
3.四种属性范围的具体介绍
示例代码如下:
attrTest1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘attrTest1.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% pageContext.setAttribute("pageContextAttr", "pageContextValue"); request.setAttribute("requestAttr", "requestValue"); session.setAttribute("sessionAttr", "sessionValue"); application.setAttribute("applicationAttr", "applicationValue"); %> <h2>attrTest1 page</h2> <br> pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %> <br><br> request: <%=request.getAttribute("requestAttr") %> <br><br> session: <%=session.getAttribute("sessionAttr") %> <br><br> application: <%=application.getAttribute("applicationAttr") %> <br><br> <a href="jspTest/attrTest2.jsp">To attrTest2 Page</a> </body> </html>
attrTest2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘attrTest2.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h2>attrTest2 page</h2> <br> pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %> <br><br> request: <%=request.getAttribute("requestAttr") %> <br><br> session: <%=session.getAttribute("sessionAttr") %> <br><br> application: <%=application.getAttribute("applicationAttr") %> <br><br> </body> </html>
运行后跳转页面显示结果如下:
从上面的运行结果来看,pageContext属性的作用范围仅限于当前JSP页面,从attrTest1页面跳转到attrTest2页面之后,pageContext对象获得的属性值为null。
修改上面的代码:
attrTest1.jsp中,做如下修改:
<h2>attrTest1 page: <%=new Date() %></h2>
attrTest2.jsp中,做如下修改:
<h2>attrTest2 page:<%=new Date() %></h2>
运行attrTest1.jsp后:
跳转到attrTest2页面:
从上面的运行结果可以看出,request的属性作用范围仅限于同一个请求,对于不同页面的不同请求的Date函数的调用,输出的时间是不同的。
运行attrTest1.jsp:
跳转到attrTest2 页面时:
关闭浏览器,直接运行attrTest2.jsp,看到session的属性值为null,运行结果显示如下:
从上面的运行结果可以看出,在一次会话(session)过程中,在第一个页面上设置的属性,跳转(服务器跳转/客户端跳转)到其他页面之后,其他的页面依然可以取得第一个页面上设置的属性。会话结束后,属性值丢失(体现为session值为null)。
标签:属性 技术分享 tle 保存 ext.get 示例 odi size doctype
原文地址:https://www.cnblogs.com/naihuangbao/p/9794792.html