Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->系统管理 ->正文

JSP专题:第六部分:用JSP实现聊天室-2

来源:Linux-cn.com 作者:Webmaster 时间:2007-05-05 点击: [收藏] [投稿]

  这几乎又是一个纯HTML的页面,在这个页面中我们使用了“"”这样的转义符,JSP中共有五个转义符:


    ddd:1到3位八进制数所表示的字符
    uxxxx:1到4位十六进制数所表示的字符
    ':单引号字符
    ":双引号字符
    \:反斜杠字符

  在很多场合,我们必须使用转义符。

  这个表单相对来说比较复杂,其输入域包含了text、select、checkbox三种类型的信息,在下面的程序中我们将看到,Java对表单信息的处理总是那么方便,而不会因为其类型的不同而有所变化。

4.7处理用户录入聊天信息的Servlet


Chat.java
package customers;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class Chat extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {response.setContentType("text/html;charset=gb2312");
     String thisMySpeaking=new String(request.getParameter("txtword")
     .getBytes("ISO-8859-1"),"GB2312");
     //处理Java对中文处理的一些问题,如果不用这两句,你可能会看到乱码
     String thisTargetUserId=request.getParameter("seluser");
     String thisColor=request.getParameter("selcolor");
     String thisFace=request.getParameter("selface");
     String selRe1=request.getParameter("selre1");
     String selRe2=request.getParameter("selre2");
     String thisRadprivate=request.getParameter("radprivate");
     if (thisRadprivate==null) thisRadprivate=new String("no");
     //接收表单的各项录入数据
     HttpSession session=request.getSession(true);
     oneUser thisUser=(oneUser)session.getAttribute("thisUser");
     String thisSourceUserId=thisUser.getUserId();
     String thisSourceUserName=thisUser.getUserName();
     int intChatroom=Integer.parseInt(thisUser.getUserPosion());
     //从session中取当前用户的相关信息
     ServletContext application=getServletContext();
  ChatRoom thischatroom=(ChatRoom)application.getAttribute("thischatroom");
     int retimeuser=Integer.parseInt(selRe2);
     int retimechat=Integer.parseInt(selRe1);
     thisUser.setRetimeUser(retimeuser,intChatroom);
     thisUser.setRetimeChat(retimechat,intChatroom);
     //按表单内容重新设置两个刷新时间
     String thisTargetUserName=thischatroom.returnUserName
     (intChatroom,thisTargetUserId);
     thischatroom.addSpeaking(thisMySpeaking,thisSourceUserId,
     thisTargetUserId,
       thisSourceUserName,thisTargetUserName,thisFace,thisColor,
       thisRadprivate,intChatroom);
//增加发言内容
     getServletConfig().getServletContext().getRequestDispatcher
     ("/jsp/mychat.jsp").forward(request,response);
//转向mychat.jsp显示
    }
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request, response);
    }
}

  正如我们前面提到的,Java对表单信息的处理相当方便,不论是什么类型,几乎是千篇一律的request.getParameter(),只是对于checkbox类型,我们有必要注意到,当其未被选中时,request.getParameter()的返回值是个空值。对于空值的逻辑判断较为特殊。因此,你最好为这样的空值置一个确定的值。

4.8显示聊天信息的页面


mychat.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<jsp:useBean id="thisUser" scope="session" 
class="customers.oneUser" />
<jsp:useBean id="thischatroom" scope="application" 
class="customers.ChatRoom" />
<%
int intchatroom=Integer.parseInt(thisUser.getUserPosion());
%>
<HTML>
   <HEAD>
      <meta http-equiv="Refresh"
      content="<%=thisUser.getRetimeChat(intchatroom)%>
      URL=/examples/jsp/mychat.jsp">
      <script language=javascript>
      var timeRun=false;
      var timeID;
      function chatScroll(){
      this.scroll(0,9999999);
      if(timeRun) clearTimeout(timeID);
      timeID=setTimeout('chatScroll()',500);
      timeRun=true;
      }function scrollStop(){
      if(timeRun) {clearTimeout(timeID); timeRun=false;}
      else chatScroll(); 
      }
      function rop(){
      setTimeout('rop()',10000);
      }
      chatScroll();
      rop();
      </script>
      //自动滚屏至页面最下端
   </HEAD>
   <BODY>
<%
  if (thisUser.getUserName() != null)
    {String UserId=thisUser.getUserId();
     String allSpeaking=thischatroom.returnAllSpeaking(intchatroom,UserId);
     out.print(allSpeaking);
     //显示聊天信息
    }
  else out.print("尚未登录,请先进入<A href='/examples/jsp/login.jsp'>
    用户登录</A>");
%>
   </BODY>
</HTML>


 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:JSP专题:第六部分:用JSP实现聊天室-1   下一篇:不同Web主机上的Servlet之间数据对象的相互传输

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号