`
沙漠魚
  • 浏览: 39763 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

JSP聊天室程序

阅读更多


?

4.1一个保存用户单条发言的类

speaking.java:
package customers;
import java.util.*;
public class speaking
? {String mySpeaking;/*发言内容*/
?? String sourceUserId;/*发出信息的用户的用户ID*/
?? String targetUserId;/*接收信息的用户的用户ID*/
?? String sourceUserName;/*发出信息的用户的用户名*/
?? String targetUserName;/*接收信息的用户的用户名*/
?? String face;/*表情*/
?? String color;/*显示颜色*/
?? String radprivate;/*是否私聊*/
?? public void setSpeaking(String theSpeaking,String theSourceUserId,
?? String theTargetUserId,
????? String theSourceUserName,String theTargetUserName,
????? String theFace,String theColor,
????? String theRadprivate)
?? //新发言内容设定
???? {mySpeaking=theSpeaking;
????? sourceUserId=theSourceUserId;
????? targetUserId=theTargetUserId;
????? sourceUserName=theSourceUserName;
????? targetUserName=theTargetUserName;
????? face=theFace;
????? color=theColor;
????? radprivate=theRadprivate;
???? }
?? public String returnSpeaking(String thisUserId)
?? //返回某一用户所能看到的该发言内容显示
???? {String thisSpeaking=new String("");
????? if (radprivate.equals("system"))
???????? {if (color.equals("red"))
???????????? thisSpeaking="(系统公告)"
???? +sourceUserName+"进入聊天室。
";
????????? else
???????????? thisSpeaking="(系统公告)"
???? +sourceUserName+"离开聊天室。
";
???????? }
???????? //约定是否私聊为“system”,显示颜色为“red”及“green”
时分别表示用户进出聊天室的系统公告,该信息对所有用户可见
????? else
?????? {if ((!radprivate.equals("yes"))' '(targetUserId.equals(thisUserId))' '
???????????? (sourceUserId.equals(thisUserId)))
???????????? {if (targetUserId.equals("all"))
???????????????? thisSpeaking=""
+sourceUserName+"对大家"+returnFace(face)+"说:
"+mySpeaking+"
";
????????????? else
???????????????? thisSpeaking=""
+sourceUserName+"对"+targetUserName+returnFace(face)+"说:
"+mySpeaking+"
";
???????????? }
???????? }
???????? //约定非私聊信息、该用户发出的信息、向该用户发出的信息、
向大家发出的信息,具备以上条件之一的聊天信息对该用户可见。
????? return thisSpeaking;
???? }
? public String returnFace(String theFace)
? //根据表情代码返回表示表情的字符串
???? {int nn;
????? nn=Integer.parseInt(theFace);
????? String thisFace=new String("");
????? switch (nn)
???????? {case 1:thisFace="笑嘻嘻地";break;
????????? case 2:thisFace="微笑着";break;
???????? }
????? return thisFace;
???? }
}
又是类,你应该看到,在Java中我们与类的接触是如此频繁,正如我们在前面提到的,在Java看来:世间万物都是类。当然,我相信,作为一个有经验的程序员,即使是在Java中,不使用类你同样可以编出功能完备的程序。但是,不论你以前对面向对象的编程怎么看,在Java中,你必须习惯这种用法。而且,从我个人而言,我觉得类是一个蛮可爱的东西。

  另外,到目前为止,我们已经多次使用到String类型的变量。在Java中变量的情况同样符合类的概念,除了为数不多的诸如int这样的简单类型变量外,绝大多数的变量都是类。而String也是一个类,对于String有它自己的许多方法,你不能用处理简单类型变量的格式来处理它,例如:if (color.equals("red")你就不能写成if (color=="red"),有一种情况例外,当你判断一个String变量是否为空值时,你可以使用if (color==null)。

4.2保存聊天室信息的类

ChatRoom.java:
package customers;
import java.util.*;
public class ChatRoom
? {speaking chatSpeaking[][];/*聊天室的全部发言内容*/
?? private int speakingCount[];/*聊天室的发言条数*/
?? private int userCount[];/*聊天室的在线人数*/
?? oneUser theUserList[][];/*聊天室的在线用户列表*/
?? public ChatRoom()
?? //构造函数
???? {theUserList=new oneUser[3][100];
????? userCount=new int[3];
????? chatSpeaking=new speaking[3][50];
????? speakingCount=new int[3];
????? userCount[1]=0;
????? userCount[2]=0;
????? speakingCount[1]=0;
????? speakingCount[2]=0;
???? }
?? public void addUserList(String theOneUserId,String theOneUserName,
?? long theOneUserTime,String theOneUserPosion,String theOneUserSwjg,
?? String theOneUserSfks,int chatroom)
?? //向在线用户列表中添加用户,如果用户已存在,将不重复添加
???? {int i;
????? for (i=1;i<=userCount[chatroom];i++) if (returnUserId(chatroom,i)
????? .equals(theOneUserId)) break;
????? if (i==userCount[chatroom]+1)
??????? {userCount[chatroom]=userCount[chatroom]+1;
???????? oneUser theOneUser=new oneUser();
???????? theOneUser.setOneUser(theOneUserId,theOneUserName,theOneUserTime,
theOneUserPosion,theOneUserSwjg,theOneUserSfks);
???????? theUserList[chatroom][userCount[chatroom]]=theOneUser;
??????? }
???? }
?? public void deleteUserList(String theOneUserId,int chatroom)
?? //从在线用户列表中删除用户
???? {int i;
????? for (i=1;i<=userCount[chatroom];i++) if (returnUserId(chatroom,i)
????? .equals(theOneUserId))
?????? theUserList[chatroom][i]=theUserList[chatroom][userCount[chatroom]];
????? userCount[chatroom]=userCount[chatroom]-1;
???? }
?? public void addSpeaking(String theSpeaking,String theSourceUserId,
?? String theTargetUserId,String theSourceUserName,String theTargetUserName,
?? String theFace,String theColor,String theRadprivate,int chatroom)
?? //增加聊天信息,如果发言条数未超过30条,则添加一条,发言条数相应加1;
?? 反之,将丢弃最先的一条发言,再将新发言添加到最后,发言条数不变
???? {speaking thisSpeaking=new speaking();
thisSpeaking.setSpeaking(theSpeaking,theSourceUserId,theTargetUserId,
theSourceUserName,theTargetUserName,theFace,theColor,theRadprivate);
????? if (speakingCount[chatroom]<=30)
???????? {speakingCount[chatroom]=speakingCount[chatroom]+1;
????????? chatSpeaking[chatroom][speakingCount[chatroom]]=thisSpeaking;
???????? }
????? else
???????? {int i;
????????? for (i=1;i<speakingcount[chatroom];i++) chatspeaking[chatroom][i]<br="">chatSpeaking[chatroom][i+1];
????????? chatSpeaking[chatroom][speakingCount[chatroom]]=thisSpeaking;
???????? }
???? }
?? public void setUserTime(String theUserId,long theUserTime,int intchatroom)
?? //设置用户时间
???? {int i;
????? for (i=1;i<=userCount[intchatroom];i++) if ((theUserList[intchatroom][i]
????? .getUserId()).equals(theUserId))
???????? {theUserList[intchatroom][i].setUserTime(theUserTime);
????????? break;
???????? }
???? }
?? public int returnUserCount(int chatroom)
?? //获取在线人数
???? {return userCount[chatroom];
???? }
?? public int returnSpeakingCount(int chatroom)
?? //获取发言条数
???? {return speakingCount[chatroom];
???? }
?? public String returnUserId(int chatroom,int i)
?? //获取聊天室中指定序号的用户的用户ID
???? {return theUserList[chatroom][i].getUserId();
???? }
?? public String returnUserName(int chatroom,int i)
?? //获取聊天室中指定序号的用户的用户名
???? {return theUserList[chatroom][i].getUserName();
???? }
?? public String returnUserName(int chatroom,String userId)
?? //获取聊天室中指定用户ID的用户的用户名
???? {String UserName=new String("");
????? int i;
????? for (i=1;i<=userCount[chatroom];i++)
???????? if (returnUserId(chatroom,i).equals(userId))
??????????? UserName=theUserList[chatroom][i].getUserName();
????? return UserName;
???? }
?? public String returnAllSpeaking(int chatroom,String thisUser)
?? //获取某用户可见的全部聊天信息
???? {int i;
????? java.util.Date dateTimenow=new java.util.Date();
????? long longDateTimeNow=(long)dateTimenow.getTime();
????? setUserTime(thisUser,longDateTimeNow,chatroom);
????? for (i=1;i<=userCount[chatroom];i++)
??????? {long oneUserTime=(long)theUserList[chatroom][i].getUserTime();
???????? if (longDateTimeNow - oneUserTime>60000)
{addSpeaking("","","",theUserList[chatroom][i].getUserName(),"","","green",
"system",chatroom);
???????????? deleteUserList(theUserList[chatroom][i].getUserId(),chatroom);
???????????? //在调用该方法时,同时判断有无超时用户,如有,将其从在线用户列
???? 表中自动删除,并发出相应系统公告
??????????? }
??????? }
????? String allSpeaking=new String("");
????? for (i=1;i<=speakingCount[chatroom];i++)
????? allSpeaking=allSpeaking+chatSpeaking[chatroom][i]
????? .returnSpeaking(thisUser);
????? return allSpeaking;
???? }
? }
在这个类中,我们使用了数组来保存各个聊天室的信息,在我们的程序中包含了两个相对独立的聊天室,而事实上采用这样的数据结构,你可以建立更多的聊天。在下面的程序中,我们将看到,我们使用了JavaBean在JSP中引用该类,而对于JSP中的JavaBean来说,其ID对应了一个实例的名称,ID是不能使用变量的,所以在这里选用这样的一个类来包容所有聊天室的信息还是一个不错的选择(如果你还不知道什么是JavaBean,你可以暂时不必理会这段话)。

  此外,在我们的returnAllSpeaking()方法中,包含了对超时用户的处理,而在这个方法中我们看到用户在调用该方法时,其用户时间将被置为当前时间,而下面我们将看到,用户对聊天室的每次刷新(包括自动刷新)都会调用该方法。所以,我们这里的所谓超时,也就意味着,用户关闭了聊天窗口,或用户失去连接一段时间。

4.3用户选择进入相应聊天室的页面

chatroom.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<!---->
<html>
?? <head>
????? <BR>???????? 聊天室--在线交流<BR>?????
?? </head>
?? <body>
?????


????? 聊天室


?????


????? <A href="/examples/jsp/index.jsp">首页<!---->>
????? <A href="/examples/jsp/chatroom.jsp">在线交流<!---->
?????


<jsp:usebean scope="session">
<!---->
<jsp:usebean scope="application">
<!---->
<%
? if (thisUser.getUserName() != null)
??? {
%>
<!---->
?????

用户
????? <%=thisUser.getUserName()%>,请选择您想要进入的聊天室
????? <!---->


?????
????????
???????????
????????
????????
???????????
????????
?????

??????????????
?????? COLOR='#0000FF'><A HREF="/examples/jsp/chat.jsp?
?????? chatroom=1">聊天室A[<%=thischatroom.returnUserCount(1)%>]
?????? <!---->

<!---->
???????????

??????????????

?????? <A HREF="/examples/jsp/chat.jsp?chatroom=2">聊天室B
?????? [<%=thischatroom.returnUserCount(2)%>]
?????? <!---->

<!---->
???????????

<%
??? }
??? else out.print("尚未登录,请先进入<A href='/examples/jsp/login.jsp'>
??? 用户登录<!---->");
%>
<!---->
?? </body>
</html>
事实上,这是我们接触到的第一个真正意义上的JSP文件,在此之前的login.jsp实在是太简单了。

  这个JSP文件初看上去具有JSP特色的东西仍然不是太多,但是它实际上基本涵盖了JSP的基本语法元素。

  “<%@ page contentType="text/html;charset=gb2312" %>” ,这是一个JSP的page指令。其中的“contentType="text/html;charset=gb2312"”定义了本页的contentType属性。利用page指令你还可以说明更多的有关本页的属性。page指令对每一个属性只能说明一次,可以放在JSP文件的任何地方,其作用范围都是整个JSP页面。不过我们一般将它放在JSP文件的顶端。

  “<jsp:usebean scope="session">”、“<jsp:usebean scope="application">”两句是JSP的useBean指令。

  useBean指令用于定位或示例一个JavaBean组件,对于JavaBean组件,你可以简单地理解为Java类,象我们这里的两个useBean指令就分别对应于我们前面定义的oneUser类及ChatRoom类。useBean指令存在较为复杂的语法的选择,我们这里使用的是一种基本的格式。

  id是在所定义的范围中确认JavaBean的变量,Java引擎通过id值识别该JavaBean是否存在,如果存在,就直接引入已存在的JavaBean;反之就会从一个class或模板中进行示例。新建的JavaBean,可以在以后的程序中通过id值来调用。正如我们前面提到的,id值只能是一个字符串型的常量,而不能是变量。

  scope是JavaBean存在的范围以及id变量名的有效范围。简单地说,session将存在于某一用户与服务器的会话中,对于该用户,除非他关闭浏览器、与服务器失去连接或服务器指定该session无效,他可以一直使用session中绑定的对象;application和session存在着某种相似的情况,它差不多是一个公用的session,虽然在原理上两者存在较大差异,但是在具体表现上的确是这样的。也就是说application中绑定的对象对于每一个用户都将是共享的,每一个用户对其进行的操作也将会影响其他用户。

class指定了该JavaBean不存在时的示例方法,同时它也指定了该JavaBean所对应的类。

  如果你想对JavaBean有更多的了解,你可以参阅我所知道的JavaBeans一文。

  由“<%、%>”引起的称为JSP的脚本程序段,和ASP、PHP类似,JSP中脚本程序的引入是通过“<%、%>”嵌入的,所以我们习惯地将这三者都称之为嵌入式脚本语言。在JSP的脚本程序段中使用的完全是Java的语法。另外,在这里我们可以注意到,正如我们在前面提到的那样,我们在这里使用thisUser来调用了我们前面创建的JavaBean。

  类似“<%=thisUser.getUserName()%>”这样的结构,我们习惯地将其称之为JSP的表达式,这实际上是一种简化的输出形式,它与“<% out.print(thisUser.getUserName())”完全等效。当然,表达式必须有一个可以输出的值。

  事实上,我们可以注意到JSP语法中包含了很多东西,而最常用的应该是我们在这里使用的page指令、useBean指令、脚本程序段、表达式。

4.4聊天室的框架页面

chat.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<html>
?? <head>
????? <BR>???????? 聊天室--在线交流--<BR>???????? <%<BR>???????? String chatroom;<BR>???????? chatroom=request.getParameter("chatroom");<BR>???????? if (chatroom.equals("1")) out.print("聊天室A"); <BR>???????? if (chatroom.equals("2")) out.print("聊天室B"); <BR>???????? int intChatroom=Integer.parseInt(chatroom);<BR>???????? %><BR>?????
?? </head>
<jsp:usebean scope="session">
<jsp:usebean scope="application" <br="">class="customers.ChatRoom" />
<%
? if (thisUser.getUserName() != null)
??? {thisUser.setPosition(chatroom);
???? thischatroom.addUserList(thisUser.getUserId(),thisUser.getUserName(),
???? thisUser.getUserTime(),thisUser.getUserPosion(),thisUser.getUserSwjg(),
???? thisUser.getUserSfks(),intChatroom);
???? thischatroom.addSpeaking("","","",thisUser.getUserName(),"","","red",
???? "system",intChatroom);
%>
//设置用户当前位置,向在线用户列表中增加该用户,
并增加一条该用户进入聊天室的系统公告
??? <frameset rows="415,111" frameborder="YES" border="1" <br="">??? framespacing="1" cols="*">
?????? <frameset cols="623,160" frameborder="YES" border="1" <br="">?????? framespacing="1" rows="*">
????????? <frame src="/examples/jsp/mychat.jsp?isret=yes">
????????? <frame src="/examples/jsp/user.jsp">
?????? </frameset>
?????? <frame scrolling="NO" src="/examples/jsp/manage.jsp">
??? </frameset>
<%
??? }
??? else out.print("尚未登录,请先进入<A href='/examples/jsp/login.jsp'>
??? 用户登录<!---->");
%>
</html>
在这个页面中,你应该可以发现这样一件事,那就是我们直接使用了request、out这两个对象,而在此之前,我们并没有对这两个对象进行任何定义。这是因为这两个都是JSP的内置对象,对于JSP的内置对象,JSP引擎对其自动进行了定义,所以你可以直接引用。JSP有五个常用的内置对象:

  request对象:客户端的相关信息

  response对象:响应给客户端的相关信息

  out对象:用来向客户端输出数据

  session对象:用来用用户保存私人信息

  application对象:用来管理整个应用程序中所有客户端共享的信息

  对于这五个对象,各自有其众多的方法。

4.5显示在线用户列表的页面

user.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<jsp:usebean scope="application" <br="">class="customers.userList" />
<jsp:usebean scope="application" <br="">class="customers.ChatRoom" />
<jsp:usebean scope="session" <br="">class="customers.oneUser" />
<%
int intchatroom=Integer.parseInt(thisUser.getUserPosion());
%>
<html>
?? <head>
????? <meta http-equiv="Refresh" content="&lt;%=thisUser.getRetimeUser&lt;BR&gt;????? (intchatroom)%&gt;URL=/examples/jsp/user.jsp">
?? </head>
?? <body>
<%
? if (thisUser.getUserName() != null)
??? {
%>
??? 本网站在线人员:
???

??? <%
?????? if (userlist!=null)
???????? {int i;
????????? for(i=1;i<=userlist.returnUserCount();i++)
??????????? {String theUser=userlist.returnUserName(i);
???????????? out.print(theUser+"
");
??????????? }
???????? }
??? %>
??? //通过一个循环,显示目前网站的所有在线用户的用户名
??? 本聊天室在线人员:
???

??? <%
?????? if (thischatroom!=null)
???????? {int i;
????????? for(i=1;i<=thischatroom.returnUserCount(intchatroom);i++)
??????????? {String theUser=thischatroom.returnUserName(intchatroom,i);
???????????? out.print(theUser+"
");
??????????? }
???????? }
??? %>
??? //通过一个循环,显示本聊天室的所有在线用户的用户名
<%
??? }
??? else out.print("尚未登录,请先进入<A href='/examples/jsp/login.jsp'>
??? 用户登录<!---->");
%>
?? </body>
</html>
在这段程序中,我们第一次引入了一个application域的名为userlist的JavaBean来说明本网站的所有在线用户。这个JavaBean虽然是第一次被引入,但是,在这里并不是第一次被创建,因为我们通过这个JavaBean来显示了当前网站的所有在线用户的用户名,情况很明显,在此之前,该JavaBean实际上已经存在,而且在该JavaBean中记录了当前网站的所有在线用户的信息。

  那么,这个JavaBean是在什么地方被定义?其中的信息又是如何被保存的呢?事实上,在我们的JSP专题:第六部分:用JSP实现聊天室-1中介绍了一个名为Login的Servlet,这个Servlet是用来处理用户登录的,在这段程序中,application绑定的名为userlist的对象就对就对应了我们现在使用的userlist这个JavaBean。同样,在chat.jsp中session域的名为thisUser的JavaBean对应了Login.java中session绑定的名为thisUser的对象。

  当你使用JSP Model 2的方式开发你的应用时,了解这样的对应关系是相当重要的,参阅我所知道的JavaBeans你会对这样的对应关系有更清晰的了解。

4.6用户录入聊天信息的页面

manage.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<html>
?? <body>
<jsp:usebean scope="session">
<jsp:usebean scope="application" <br="">class="customers.ChatRoom" />
<%
? if (thisUser.getUserName() != null)
??? {
%>


cellpadding="5" align="center">
??
?????
?????
??

????????

聊天内容:
???????????
??????????? ????对象:
??????????? ?????????????? 大家?????????????? <%???????????????? int intchatroom=Integer.parseInt(thisUser.getUserPosion());???????????????? int i;???????????????? for(i=1;i<=thischatroom.returnUserCount(intchatroom);i++) ?????????????? {String theUserName=thischatroom.returnUserName(intchatroom,i);?????????????? String theUserId=thischatroom.returnUserId(intchatroom,i);???????????????????? if (!thisUser.getUserId().equals(theUserId))??????????????????? out.print(""??? +theUserName+"");??????????????????????? //此处使用了转义符??????????????????? }?????????????? %>???????????
??????????? ????颜色:
??????????? ?????????????? 蓝色?????????????? 黑色???????????
????????

表情:
??????????? ?????????????? 无?????????????? 笑嘻嘻?????????????? 微笑着???????????
??????????? ?聊天刷新:
??????????? ?????????????? ?????????????? <%?????????? if (thisUser.getRetimeChat(intchatroom)==5) out.print("selected"); ?????????????? %>?????????????? >5秒?????????????? ?????????????? <%?????????? if (thisUser.getRetimeChat(intchatroom)==10) out.print("selected"); ?????????????? %>?????????????? >10秒???????????
??????????? ?用户刷新:
??????????? ?????????????? ?????????????? <%?????????? if (thisUser.getRetimeUser(intchatroom)==5) out.print("selected"); ?????????????? %>?????????????? >5秒?????????????? ?????????????? <%?????????? if (thisUser.getRetimeUser(intchatroom)==10) out.print("selected"); ?????????????? %>?????????????? >10秒???????????
??????????? ?
???????????
??????????? 私聊
??????????? ?
???????????
??????????? ?
??????????? <A href="../servlet/customers.Bye" target="_parent">
??? 离开聊天室<!---->
??????????? ?
??????????? <A href="/examples/jsp/manage.jsp">刷新<!---->
???????????


????????


<%
??? }
??? else out.print("尚未登录,请先进入<A href='/examples/jsp/login.jsp'>
??? 用户登录<!---->");
%>
?? </body>
</html>
这几乎又是一个纯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 scope="session" <br="">class="customers.oneUser" />
<jsp:usebean scope="application" <br="">class="customers.ChatRoom" />
<%
int intchatroom=Integer.parseInt(thisUser.getUserPosion());
%>
<html>
?? <head>
????? <meta http-equiv="Refresh" <br="">????? 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'>
??? 用户登录<!---->");
%>
?? </body>
</html>
这段程序中最主要的是一段用来自动滚屏至页面最下端的Java Script,我们可以看到无论是对于JSP,还是对于ASP和PHP,Java Script的使用总是不可避免。而在这三者中使用Java Script都不复杂,就象你在HTML文件中使用时一样。

  处理用户离开聊天室的Servlet

Bye.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 Bye extends HttpServlet {
??? public void doGet(HttpServletRequest request,
????????????????????? HttpServletResponse response)
??????? throws IOException, ServletException
??? {response.setContentType("text/html;charset=gb2312");
???? HttpSession session=request.getSession(true);
???? oneUser thisUser=(oneUser)session.getAttribute("thisUser");
???? int intChatroom=Integer.parseInt(thisUser.getUserPosion());
???? ServletContext application=getServletContext();
?? ChatRoom thischatroom=(ChatRoom)application.getAttribute("thischatroom");
???? thischatroom.addSpeaking("","","",thisUser.getUserName(),
???? "","","green","system",intChatroom);
//增加用户离开聊天室的系统公告
???? thischatroom.deleteUserList(thisUser.getUserId(),intChatroom);
???? thisUser.setPosition("0");
//从在线用户列表中删除该用户,并重新设置用户当前位置
???? getServletConfig().getServletContext().getRequestDispatcher
???? ("/jsp/chatroom.jsp").forward(request,response);
//转向chatroom.jsp
??? }
??? public void doPost(HttpServletRequest request,
????????????????????? HttpServletResponse response)
??????? throws IOException, ServletException
??? {
??????? doGet(request, response);
??? }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics