⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chat_sendmsg.java

📁 jsp 应用开发详解源代码 电子工业出版社 包含书中个源代码
💻 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 (boolcanupdatemsg==true)    {            tempchatMsg.addElement(cUserName+"对"+talkTo+"说:"+temp);            getServletContext().setAttribute("chatMsg",tempchatMsg);    }    out.println("<form action=\"\" method=\"post\" onsubmit=\"setCookie()\" id=\"form1\">");    out.println("   <table border=\"0\" cellspacing=\"3\" cellpadding=\"0\">");    out.println("    <tr>");    out.println("	  <td>"+session.getValue("username")+"</td>");    out.println("      <td align=\"left\" colspan=\"2\"> ");    out.println("              <input name=\"message\" style=\"HEIGHT: 21px; WIDTH: 367px\">");    out.println("       </td>");    out.println("    </tr>");    out.println("    <tr>   ");    out.println("      <td align=\"center\"> ");    out.println("	  对象          ");    out.println("      </td>");    out.println("	  <td align=\"left\"> ");    out.println("              <!--在一个下拉列表中显示所有用户名供用户选择-->");    out.println("              <select name=\"toTalk\">       ");    out.println("                <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>");    }    out.println("              </select>");    out.println("			  <input type=\"submit\" value=\"说话呀\" name=\"submit\">");    out.println("		</td>");    out.println("		<td>");    out.println("			   <a href=\"ec.chat_leavechat\" target=\"_top\">离开聊天室</a>");    out.println("       </td>");    out.println("    </tr>");    out.println("  </table>");    out.println("</form> ");    out.println("</body>");    out.println("</html>");  }  /**Clean up resources*/  public void destroy() {  }}

⌨️ 快捷键说明

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