JSP application. Setattribute() method: set attribute
•
Java
setAttribute(String name,Object object)
Example
<%
application.setAttribute("user","lzw");
Date Now = new Date();
application.setAttribute("Now",Now);
%>
Typical application
<body>
<p>application实现简单计数器<br>
<%
Integer count = null; //定义用于表示计数器的Integer对象
synchronized(application){ //同步处理,保证只有一个进程可访问本方法
count = (Integer)application.getAttribute("counter"); //获取保存在application对象中的信息
if(count==null) //判断该变量是否为空
count = new Integer(0);
count = new Integer(count.intValue()+1); //将变量进行加1处理
application.setAttribute("counter",count); //将变量保存在application对象中
}
%>
<font size=6 color="blue">您好!欢迎访问本站,您是第<%=count%>位访客</font>>
</body>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
