login.html
<html>
<body>
<font size = "16">
<form action = "chat.jsp" method = "post">
<center>
用戶名: <input type = "text" name = "username"><br>
密碼:<input type = "password" name = "password"><br>
<input type = "submit" value = "提交"><br>
</form>
</font>
</body>
</html>
chat.jsp
<%@ page contentType="text/html;Charset=gbk" import="java.util.*"%>
<%
String username = request.getParameter("username");
/*先檢查有沒有hash表*/
HashMap hs =(HashMap)application.getAttribute("userlist");/*application對象是用服務器創建所有用戶共享,getAttribute檢索與此修改項關聯的屬性。
返回:
用于修改的非 null 屬性。*/
if(hs==null)/*如果沒有,創建一個*/
hs = new HashMap();
hs.put(username,username);/*把人加入到hash表*/
/*把hash表放到appplication中*/
application.setAttribute("userlist",hs);//將參數添加到application中
response.sendRedirect("userlist.jsp");//客戶重定向
%>
usetlist.jsp
<%@ page contentType="text/html;Charset=gbk" import="java.util.*"%>
<meta http-equiv="refresh" content="3; url=userlist.jsp">/*3秒后跳轉到本頁面,用戶端拉 <META HTTP-EQUIV="Refresh" CONTENT="?; URL=URL">(使用端自動更新 )*/
<html>
<body>
<font size="20">
<%
out.print("用戶列表如下"+"<Br>");
HashMap hs= (HashMap)application.getAttribute("userlist");
Iterator it = hs.keySet().iterator();/*Iterator對集合進行迭代的迭代器,這個Keyset.iterator()迭代器是將hs中的元素按順序讀出來的*/
while(it.hasNext())//hasNext()如果仍有元素可以迭代,則返回 true。
{
Object key = (Object)it.next();//返回迭代的下一個元素。
Object username =(Object)hs.get(key);
out.print(username);
out.print("<br>");
}
%>
</body>
</html>
posted on 2009-05-03 11:08
鵬凌 閱讀(103)
評論(0) 編輯 收藏