【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了chat_sendmsg.java提供大家参考,希望对大家有所帮助!
package ec;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class chat_sendmsg extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
/**Initialize global variables*/
public void init() throws ServletException {
}
//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>");
}
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>chat_sendmsg</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
HttpSession session=request.getSession(true);
out.println("<html>");
out.println(" <title>发送聊天信息</title>");
out.println("<head>");
out.println("<script language=\"JavaScript\" type=\"text/javascript\">");
out.println("function setCookie()");
out.println("{");
out.println(" document.cookie=form1.toTalk.selectedIndex+\"#\";");
out.println("}");
out.println("function setFocus()");
out.println("{");
out.println(" form1.message.focus();");
out.println(" var cookieValue=document.cookie;");
out.println(" var formValue=cookieValue.split(\"#\");");
out.println(" form1.toTalk.selectedIndex=formValue[1];");
out.println(" _formValue=formValue[0].split(\";\");");
out.println("}");
out.println("</script>");
out.println("</head>");
out.println("<body bgcolor=\"#CCCCFF\" onload=\"setFocus()\">");
//***********************************************************************************************
//初始化一些
常用的变量
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 (boolcanupdate