sendmsg.jsp

来自「jsp的技术教程」· JSP 代码 · 共 146 行

JSP
146
字号
<%@ page import="java.util.Vector" %>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Date" %>
<html>
<title>发送聊天信息</title>
<head>
<script language="JavaScript" type="text/javascript">
function  setCookie()
{
	document.cookie=form1.toTalk.selectedIndex+"#";
}
function setFocus()
{
	form1.message.focus();
	var cookieValue=document.cookie;
	var formValue=cookieValue.split("#");
	form1.toTalk.selectedIndex=formValue[1];
	_formValue=formValue[0].split(";");
}
</script>
</head>
<body bgcolor="#CCCCFF" onload="setFocus()">
<%!
//define a useful function which will be used by the following code 
public void clearVector()
{
	Vector _tempchatMsg=(Vector)getServletContext().getAttribute("chatMsg");
	
	if (_tempchatMsg.size()>40)
	{
		_tempchatMsg.removeAllElements();
		_tempchatMsg.addElement("<font color='orange' size='2'>"+"系统管理员清除了消息队列,大家慢聊"+".</font><br>");
	}
}
%>

<%
//***********************************************************************************************
//初始化一些常用的变量
boolean boolcanupdatemsg=true;
Vector tempchatMsg=(Vector)getServletContext().getAttribute("chatMsg");
String cUserName=(String)session.getAttribute("username");
Date userTime=new Date();
String timestamp="["+userTime.getHours()+":"+userTime.getMinutes()+":"+userTime.getSeconds()+"]";
//***********************************************************************************************
%>

<%
//***********************************************************************************************
//receive some parameter and analysis them 
byte[] temp_t;
String temp_p;
temp_p=request.getParameter("message");
temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);

byte[] talkTo_t;
String talkTo_p;
talkTo_p=request.getParameter("toTalk");
talkTo_t=talkTo_p.getBytes("ISO8859-1");
String talkTo=new String(talkTo_t);
%>
<%
//analysis the parameter
if(temp.startsWith("<")) 
{
	temp="<font color='red' size='2'>请不要使用html标记"+timestamp+"</font><br>";
	tempchatMsg.addElement(temp);
	boolcanupdatemsg=false;
	getServletContext().setAttribute("chatMsg",tempchatMsg);
}

if (temp.endsWith("/>"))
{
	temp="<font color='red' size='2'>请不要使用html标记"+timestamp+"</font><br>";
	tempchatMsg.addElement(temp);
	boolcanupdatemsg=false;
	getServletContext().setAttribute("chatMsg",tempchatMsg);
	
}

if (talkTo.equals("all"))
{
	talkTo="每一个人";
}
%>


<%
cUserName="<font color='red' size='2'>"+cUserName+"</font>";
talkTo="<font color='#0000FF' size='2'>"+talkTo+"</font>";

if (boolcanupdatemsg==true)
{
	//update some msg so that the user have the asbility to update the msg 
	Hashtable userLife=(Hashtable)getServletContext().getAttribute("userLife");
	userLife.put((String)session.getValue("username"),new Long(userTime.getTime()));
	getServletContext().setAttribute("userLife",userLife);
	//**************************************************************
}
if (boolcanupdatemsg==true)
{
	tempchatMsg.addElement(cUserName+"对"+talkTo+"说:"+temp);
	getServletContext().setAttribute("chatMsg",tempchatMsg);
	
}
%>
<form action="sendmsg.jsp" method="post" onsubmit="setCookie()" id="form1">
   <table border="0" cellspacing="3" cellpadding="0">
    <tr>
	  <td><%=session.getValue("username")%></td>
      <td align="left" colspan="2"> 
              <input name="message" style="HEIGHT: 21px; WIDTH: 367px">
       </td>
    </tr>
    <tr>   
      <td align="center"> 
	  对象           
      </td>
	  <td align="left"> 
              <!--在一个下拉列表中显示所有用户名供用户选择-->
              <select name="toTalk">             
                <option value="all">全体在聊</option>
                <%
					Vector tempuser=(Vector)getServletContext().getAttribute("chatUser");
					for (int i=0;i<tempuser.size();i++)
					{
						out.println("<option>");
						out.println(tempuser.elementAt(tempuser.size()-i-1).toString());
						out.println("</option>");
					}

				%> 
              </select>
			  <input type="submit" value="说话呀" name="submit">
		</td>
		<td>
			   <a href="leavechat.jsp" target="_top">离开聊天室</a>
       </td>
    </tr>
  </table>
</form> 
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?