<html>
<body>
<%
//application对象--javax.servlet.ServletContext
//主要的作用就是保存公共信息
//属性操作三种方法:
setAttribute(),
getAttribute(),取得的是Object类型,可以进行强制类型转换,
如:String uname=(String)getAttribute("name")
removeAttribute()
//释放application属性,重新启动服务器则属性值无效
//此工作目录的路径是不固定的,you可能次工作目录在C盘或者D盘
//得到工作目录的真实路径方法:public String getRealPath(String path)
//与session相似用来保存信息,但是session是每个用户私有的,而Application是大家共有的
//在实际应用中往往使用方法:
getServletContext()//代替application
//开发原则:HTML代码和JAVA代码分离,如果要输出尽量使用表达式输出<%=%>
<%//尽量不使用jsp内置对象out,会破坏数据结构
%>
<h1><%=getServletContext.getRealPath()%></h1>
</body>
</html>
四种属性有效范围
{setAttribute(),getAttribute(),removeAttribute()}
pageContext-----------------单个页面,进行任何跳转属性失效
request——————————————————————一个地址,只能是服务器端跳转
------------------------------这两种使用最多-----------------------------------------
session——————————————————————一个浏览器页面,可以随意跳转一直有效
application——————————————————一个服务器,所有网页窗口属性都有效,
有效范围越大则系统开销越大,所以尽量使用有效范围小的属性。
实际上所有的属性范围都是通过pageContext对象实现的,只是为了方便区分,换为四个不同的对象
使用方法:setAttribute(String name,Object value, int scope)实现四种不同的属性范围
pageContext.setAttribute("name","zhangmingbao", pageContext.REQUEST_SCOPE)
pageContext.setAttribute("name","zhangmingbao", pageContext.SESSION_SCOPE)
pageContext.setAttribute("name","zhangmingbao", pageContext.APPLICATION_SCOPE)
<body>
<%
//application对象--javax.servlet.ServletContext
//主要的作用就是保存公共信息
//属性操作三种方法:
setAttribute(),
getAttribute(),取得的是Object类型,可以进行强制类型转换,
如:String uname=(String)getAttribute("name")
removeAttribute()
//释放application属性,重新启动服务器则属性值无效
//此工作目录的路径是不固定的,you可能次工作目录在C盘或者D盘
//得到工作目录的真实路径方法:public String getRealPath(String path)
//与session相似用来保存信息,但是session是每个用户私有的,而Application是大家共有的
//在实际应用中往往使用方法:
getServletContext()//代替application
//开发原则:HTML代码和JAVA代码分离,如果要输出尽量使用表达式输出<%=%>
<%//尽量不使用jsp内置对象out,会破坏数据结构
%>
<h1><%=getServletContext.getRealPath()%></h1>
</body>
</html>
四种属性有效范围
{setAttribute(),getAttribute(),removeAttribute()}
pageContext-----------------单个页面,进行任何跳转属性失效
request——————————————————————一个地址,只能是服务器端跳转
------------------------------这两种使用最多-----------------------------------------
session——————————————————————一个浏览器页面,可以随意跳转一直有效
application——————————————————一个服务器,所有网页窗口属性都有效,
有效范围越大则系统开销越大,所以尽量使用有效范围小的属性。
实际上所有的属性范围都是通过pageContext对象实现的,只是为了方便区分,换为四个不同的对象
使用方法:setAttribute(String name,Object value, int scope)实现四种不同的属性范围
pageContext.setAttribute("name","zhangmingbao", pageContext.REQUEST_SCOPE)
pageContext.setAttribute("name","zhangmingbao", pageContext.SESSION_SCOPE)
pageContext.setAttribute("name","zhangmingbao", pageContext.APPLICATION_SCOPE)