jsp之application

1、session:一个client在访问网站期间共享

2、application:多个client访问网站期间共享

3、pageContext:一个页面之间共享

4、equest:请求

application.setAttribute("属性名","属性值");

application.getAttribute("属性名");

application.removeAttribute("属性名");

application.getAttributeNames();返回数据类型为:Enumeration

getMajorVersion

功能:返回服务器解释引擎所支持的最新Seervlet API版本

getMimeType(String file)

功能:返回文件file的文件格式与编码方式;

getRealPath(String path)

功能:返回虚拟路径path的真实路径;

getServerInfo()

功能:返回服务器解释引擎的信息

5、Enumeration接口

Enumertion接口中仅定义了下面两个方法。
·boolean hasMoreElemerts()
测试Enumeration枚举对象中是否还含有元素,如果返回true,则表示还含有至少一个的元素。
·Object nextElement()
如果Bnumeration枚举对象还含有元素,该方法得到对象中的下一个元素。

准备工作就绪。

现在实现统计网站的访问量,并且还要打印出网站所有访问者的sessionID

 

<%@ page import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%! int numbers = 0;%>
<%! public synchronized void countPeople(){
        numbers++;
}%>
<%
        if(session.isNew()){
                countPeople();
                String str = String.valueOf(numbers);
                session.setAttribute("count",str);
        }
        application.setAttribute(session.getId(),Integer.toString(numbers));
        Enumeration e = application.getAttributeNames();
        while(e.hasMoreElements()){
                out.println(e.nextElement().toString()+"
");
        }
%>

你的sessionID为<%=session.getId()%> 你是第<%=(String)session.getAttribute("count")%>个访问本站的人。
出现的问题:该开始没有引用java.util.*这个包,导致Enumeration不能用。以后写的时候要注意加上包了,

文章来源:http://www.2cto.com/kf/201504/390692.html

posted @ 2015-05-28 09:35  蚂蚁的天空  阅读(1402)  评论(0编辑  收藏  举报