1、
String words=("<font color="+color+">"+user+"</font>"+action+"对<font color="+color+">"+sayto+"</font>说:<font color="+color+">"+word+"</font>");
Vector msg=new Vector();
msg.addElement(words);
getServletContext().setAttribute("msg",msg);
Vector user_table=new Vector();
user_table.addElement("user");
getServletContext().setAttribute("user_table",user_table);以上代码中的msg.addElement(words);只保存words变量中值,因为你重新实例化了一个Vector msg=new Vector();你的user_table也是一样的。
你应该先用
Vector msg=new Vector();
msg = getServletContext().getAttribute("msg");
msg.addElement(words);
getServletContext().setAttribute("msg",msg);
Vector user_table=new Vector();
user_table = getServletContext().getAttribute("user_table");
user_table.addElement("user");
getServletContext().setAttribute("user_table",user_table);2、
getServletContext().setAttribute("msg",msg);
msg=(Vector)getServletContext().getAttribute("msg");
for (int i=msg.size();i>0; i--)
{
out.print(msg.elementAt(i-1));
out.print("<br>");你也应该:
Vector msg=new Vector();
msg = getServletContext().getAttribute("msg");
for (int i=msg.size();i>0; i--)
{
out.print(msg.elementAt(i-1));
out.print("<br>");
}
String words=("<font color="+color+">"+user+"</font>"+action+"对<font color="+color+">"+sayto+"</font>说:<font color="+color+">"+word+"</font>");
Vector msg=new Vector();
msg.addElement(words);
getServletContext().setAttribute("msg",msg);
Vector user_table=new Vector();
user_table.addElement("user");
getServletContext().setAttribute("user_table",user_table);以上代码中的msg.addElement(words);只保存words变量中值,因为你重新实例化了一个Vector msg=new Vector();你的user_table也是一样的。
你应该先用
Vector msg=new Vector();
msg = getServletContext().getAttribute("msg");
msg.addElement(words);
getServletContext().setAttribute("msg",msg);
Vector user_table=new Vector();
user_table = getServletContext().getAttribute("user_table");
user_table.addElement("user");
getServletContext().setAttribute("user_table",user_table);2、
getServletContext().setAttribute("msg",msg);
msg=(Vector)getServletContext().getAttribute("msg");
for (int i=msg.size();i>0; i--)
{
out.print(msg.elementAt(i-1));
out.print("<br>");你也应该:
Vector msg=new Vector();
msg = getServletContext().getAttribute("msg");
for (int i=msg.size();i>0; i--)
{
out.print(msg.elementAt(i-1));
out.print("<br>");
}
chatframa.jsp
<%@ page session="true"%>
<%@ page langue="javaScript"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.Vector"%>
<%@ page import="java.util.*"%>
<HTML>
<HEAD>
<title>Frames</title>
</HEAD><FRAMESET COLS="85,15">
<FRAMESET ROWS="80%,20%" >
<FRAME SRC="senda.jsp" name="up" scrolling="yes">
<FRAME SRC="saya.jsp" name="dowm" scrolling="yes" >
</FRAMESET>
<FRAMESET ROWS="70%">
<FRAME SRC="user_list.jsp" name="left" scrolling="auto">
</FRAMESET>
</FRAMESET>
</HTML>
saya.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>发言区</title>
<meta http-equiv="Content-Type" Content="text/html;charset=gb2312">
<link rel="stylesheet" href="../shop.css" type="text/css">
<script language="javascript">
function bs(){
document.forms.forml.word.style.color=document.forms.forml.color.value;
document.forms.forml.word.focus();
}
function check_word()
{
if(forml.word.value=="")
{
document.forms.forml.word.focus();
alert("请不要发送空信息!");
return false;
}
return true;
}
</script>
</head>
<body bgcolor="#33ccff" text="#000000" leftmargin="0"topmargin="3"onload='document.forms.forml.word.focus();'>
<form name="forml" method="post" action="senda.jsp"target="up"onsubmit="return check_word();">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="89%">发言内容
<input type="text" name="word" size="50">
<input type="submit" name="Submit" value="发言">
</td>
<td width="11%">
<Iframe name="say" height=0 width=0 src="senda.jsp"></Iframe></td>
</tr>
<tr>
<td width="89%">昵称
<input type=hidden name="nickname">
<input type="text" name="user"size="5">
对象
<input type="hidden" name="sayto" size="6" value="大家">
<input type="text" name="saytobax" size="6" value="大家"onfocus="blur()">
动作
<select name="action" size="1">
<option selected value="叫嚷着">叫嚷</option>
<option value="说话">说话</option>
<option value="拥抱">拥抱</option>
</select>
趣语
<select onchange="word.value=this.value;top.bottom.forml.word.focus();"style="font-size:9pt">
<option value selected>没有</option>
<option value="举双脚赞成">赞成</option>
<option vslue="抓了抓头皮,露出迷惑的表情">迷惑</option>
</select>
字体颜色
<select name='color' id='color' onchange='bs();'>
<option style="color:#000000" value="000000" selected>绝对黑色</option>
<option style="color:#000088"value="000088">深蓝忧郁</option>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>senda.jsp
<%@ page session="true"%>
<%@ page language="java"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.Vector"%><%
String user=request.getParameter("user");String word=request.getParameter("word");String action=request.getParameter("action");
if(action==null)
action="";
String color=request.getParameter("color");
if(color==null)
color="#000000";
String sayto=request.getParameter("sayto");
if(sayto==null)
sayto="大家";String words=("<font color="+color+">"+user+"</font>"+action+"对<font color="+color+">"+sayto+"</font>说:<font color="+color+">"+word+"</font>");
Vector msg=new Vector();
getServletContext().setAttribute("msg",msg);
msg.addElement(words);
msg=(Vector)getServletContext().getAttribute("msg");
Vector user_table=new Vector();
getServletContext().setAttribute("user_table",user_table);
user_table.addElement("user");
user_table=(Vector)getServletContext().getAttribute("user");
out.print(msg);
%>
<script language="javascript">
top.bottom.forml.word.value="";
top.bottom.forml.word.focus();
</script>
maia.jsp
<%@ page session="true">
<%@ page language="java"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.Vector"%>
<html>
<head>
<title>聊天窗口</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312"></head><%
Vector msg=new Vector();
msg=(Vector)getServletContext().getAttribute("msg");
for (int i=msg.size();i>0; i--)
{
out.print(msg.elementAt(i-1));
out.print("<br>");}
%>
</body>
</html
[email protected]
getServletContext().setAttribute("msg",msg);
msg.addElement(words);
这段代码好象每次发言都会将以前的信息给覆盖了吧?
因为每次都将msg名称的属性都赋成新的值msg。
newapparticipant.html(进入聊天室)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Star Chatroom </TITLE>
</HEAD><BODY bgcolor="#fffooo" >
<center>
<font size=+3>
<b><i>豆豆</i> 聊天室</b>
<br><br>
</font>
</center>
<font size=+0>
欢迎光临<b><i>豆豆</i></b>聊天室,
<br>
请选择您的称呼,然后单击<b><i>加入</i></b>
</font>
<br>
<center>
<form name="newuser" method=post action="chu.html">
<b>称呼</b>
<input name="username" type="text" maxlength="9" >
<br>
<input type="submit" name="submit" value="加入">
</form>
</center>
</BODY>
</HTML>
chu.jsp(初始化)
<%@ page import="java.util.Vector"%>
<%@ page import="java.util.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<%
String user=request.getParameter("user");
Vector user_list=new Vector();
user_list=(Vector)getServletContext().getAttribute("user_list");
if(user_list==null)
user_list=new Vector();
boolean have=false;
int i;
for(i=0;i<user_list.size();i++)
{
if(user.equals(user_list.elementAt(i)))
have=true;
}
if(!have)
{
user_list.addElement(user);
getServletContext().setAttribute("user_list",user_list);
}
Vector msg=new Vector();
msg=(Vector)getServletContext().getAttribute("msg");
if(msg==null)
msg=new Vector();
msg.addElement("<font color='red'>"+user+"来了,大家好好的对他哦.</font>");
getServletContext().setAttribute("msg",msg);
response.sendRedirect("chatframa.html");
%>
</html>
saya.jsp(发言部分)
<%@ page contentType="text/html;charset=gb2312"%><html>
<head>
<title>发言区</title>
<meta http-equiv="Content-Type" Content="text/html;charset=gb2312">
<link rel="stylesheet" href="../shop.css" type="text/css">
<script language="javascript">
function bs(){
document.forms.forml.word.style.color=document.forms.forml.color.value;
document.forms.forml.word.focus();
}
function check_word()
{
if(forml.word.value=="")
{
document.forms.forml.word.focus();
alert("请不要发送空信息!");
return false;
}
return true;
}
</script>
</head>
<body bgcolor="#33ccff" text="#000000" leftmargin="0"topmargin="3"onload='document.forms.forml.word.focus();'>
<form name="forml" method="post" action="senda.jsp"target="up"onsubmit="return check_word();">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="89%">发言内容
<input type="text" name="word" size="50">
<input type="submit" name="Submit" value="发言">
</td>
<td width="11%">
<Iframe name="say" height=0 width=0 src="mai.jsp"></Iframe></td>
</tr>
<tr>
<td width="89%">昵称
<input type=hidden name="nickname">
<input type="text" name="user"size="5">
对象
<input type="hidden" name="sayto" size="6" value="大家">
<input type="text" name="saytobax" size="6" value="大家"onfocus="blur()">
动作
<select name="action" size="1">
<option selected value="叫嚷着">叫嚷</option>
<option value="说话">说话</option>
<option value="拥抱">拥抱</option>
</select>
趣语
<select onchange="word.value=this.value;top.bottom.forml.word.focus();"style="font-size:9pt">
<option value selected>没有</option>
<option value="举双脚赞成">赞成</option>
<option vslue="抓了抓头皮,露出迷惑的表情">迷惑</option>
</select>
字体颜色
<select name='color' id='color' onchange='bs();'>
<option style="color:#000000" value="000000" selected>绝对黑色</option>
<option style="color:#000088"value="000088">深蓝忧郁</option>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
senda.jsp(传输部分)
<%@ page session="true"%>
<%@ page language="java"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.Vector"%><%
String user=request.getParameter("user");String word=request.getParameter("word");String action=request.getParameter("action");
if(action==null)
action="";
String color=request.getParameter("color");
if(color==null)
color="#000000";
String sayto=request.getParameter("sayto");
if(sayto==null)
sayto="大家";String words=("<font color="+color+">"+user+"</font>"+action+"对<font color="+color+">"+sayto+"</font>说:<font color="+color+">"+word+"</font>");
Vector msg=new Vector();
getServletContext().setAttribute("msg",msg);
msg.addElement(words);
msg=(Vector)getServletContext().getAttribute("msg");
Vector user_table=new Vector();
getServletContext().setAttribute("user_table",user_table);
user_table.addElement("user");
user_table=(Vector)getServletContext().getAttribute("user");
out.print(msg);
%>
<script language="javascript">
top.bottom.forml.word.value="";
top.bottom.forml.word.focus();
</script>mai.jsp(显示部分)
<%@ page session="true"%>
<%@ page language="java"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.Vector"%>
<html>
<head>
<title>聊天窗口</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<%
Vector msg=new Vector();
msg=(Vector)getServletContext().getAttribute("msg");
for (int i=msg.size();i>0; i--)
{
out.print(msg.elementAt(i-1));
out.print("<br>");}
%>
</body>
</html>user_list.jsp(用户列表部分)
<%@ page import="java.util.Vector"%>
<html>
<head>
<title>用户窗口</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312"></head>
<b> 在线聊友<b><%
Vector user_table=new Vector();
user_table.addElement("所有人");
getServletContext().setAttribute("user_table",user_table);
user_table=(Vector)getServletContext().getAttribute("user_table");
int i;
for(i=0;i<user_table.size();i++)
{
String user=(String)user_table.elementAt(i);
out.print(">><a href=javascript:saytobax('"+user+"');>");
out.print(user);
out.print("</a><<");
}
%>