聊天室

1.框架页

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 <style type="text/css">
 9     .red{
10         color:red;
11     }
12 </style>
13 </head>
14 <!-- 框架页 -->
15 <!-- 首先分上下两部分  -->
16 <frameset rows="75%,*">
17     <!-- 上面的页面又分左右两部分 -->
18     <frameset cols="75%,*">
19         <frame src="recoder.jsp"></frame>
20         <frame src="userList.jsp"></frame>
21     </frameset>
22     <frame src="message.jsp"></frame>
23 </frameset>
24 </html>

 

2.登陆页

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>登陆页</title>
 8 </head>
 9 <body>
10 <h1>欢迎进入xxx聊天室</h1>
11 <form action="doAction.jsp" method="post">
12     用户名:<input type="text" name="txtName"/><br/>
13&nbsp;&nbsp;码:<input type="password" name="txtPwd"/><br/>
14     <button type="submit" value="Login">登录</button>
15 </form>
16 </body>
17 </html>

 

3.聊天记录页

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>发送聊天信息</title>
 8 </head>
 9 <body>
10 <h1>发送聊天信息</h1>    
11 <!-- 这里要注意,点击发送按钮后,提交给当前页来处理发送信息的业务 -->
12 <form action="message.jsp" method="post">
13     <input type="text" size="40" name="txtMessage"/><input type="submit" value="发送"/>
14 </form>
15 <%
16     request.setCharacterEncoding("utf-8");
17     //点击发送,将当前用户的聊天信息保存到application对象中,以便其他用户也可访问
18     String message = request.getParameter("txtMessage");
19     if(null == message){//【特别注意这里的逻辑!!!】
20         return;
21     }
22     //到全局对象中拿到历史聊天记录,(record是我们自己定义的聊天记录的关键字,可以自行更改)
23     Object objRecord = application.getAttribute("record");
24     StringBuilder record = null;
25     if(null == objRecord){//证明还没人聊天
26         //构建新的聊天记录对象
27         record = new StringBuilder();
28         //将新聊天记录对象放在全局对象中,以便下次调用
29         application.setAttribute("record", record);
30     }else{
31         //如果聊天记录对象不为空,就去出来,并进行强转,以便后续的使用
32         record = (StringBuilder)application.getAttribute("record");
33     }
34     //将当前用户的聊天记录拼接在全局变量中
35     Object objUser = session.getAttribute("loginName");
36     record.append("<strong>" + objUser + "</strong>:<span class='red'>" + message + "</span><br/>");
37     //将聊天记录对象放回到application中
38     application.setAttribute("record", record);
39 %>
40 </body>
41 </html>

 

4.发送聊天信息页

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <meta http-equiv="Refresh" content="3">
 8 <title>聊天记录页</title>
 9 </head>
10 <body>
11 <h1>聊天记录页</h1>
12 <%
13     //接受聊天记录并显示
14     Object objRecord = application.getAttribute("record");
15     StringBuilder record = new StringBuilder();
16     if(null != objRecord){
17         record = (StringBuilder)objRecord;
18     }
19 %>
20 <%=record %>    
21     
22 </body>
23 </html>

 

5.用户列表页

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>用户列表页</title>
 8 </head>
 9 <body>
10 <h1>用户列表页</h1>
11 <%
12     request.setCharacterEncoding("utf-8");
13     //接受好友并显示
14     //将用户好友列表信息保存到application对象中,以便其他用户也可以访问
15     Object objUserList = application.getAttribute("loginName");
16     StringBuilder userList = null;
17     if(null == objUserList){
18         //如果没有好友列表,就构建新的好友列表
19         userList = new StringBuilder();
20         //将新好友列表对象放在全局对象中,以便下次调用
21         application.setAttribute("loginName", userList);
22     }else{
23         //如果好友列表对象不为空,就取出来,并进行强转,以便后续的使用
24         userList = (StringBuilder)application.getAttribute("loginName");
25     }
26     //将当前用户的聊天记录拼接在全局变量中
27     Object objUser = session.getAttribute("loginName");
28     userList.append("<strong>" + objUser + "</strong><br/>");
29     out.print(userList);
30 %>
31 </body>
32 </html>

 

6.处理业务页

 1 <%@page import="java.util.ArrayList"%>
 2 <%@page import="java.util.List"%>
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"
 4     pageEncoding="UTF-8"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>处理登录</title>
10 </head>
11 <body>
12 <%
13     //省略验证代码
14     //假设用户是合法的
15     request.setCharacterEncoding("utf-8");
16     String name = request.getParameter("txtName");
17     String pass = request.getParameter("txtPwd");
18     if(null != name || "".equals(name) || null != pass || "".equals(pass) ){
19         //如果是合法用户,就分配给用户一个身份,并且将用户的信息保存到session对象中,以便其他页面可以共享
20         session.setAttribute("loginName", name);
21         session.setAttribute("password", pass);
22         //注销
23         //session.invalidate();
24         response.sendRedirect("main.jsp");
25     }else{
26         response.sendRedirect("login.jsp");
27     }
28 %>
29 </body>
30 </html>

 

posted @ 2016-05-10 21:53  Ivy_Xu  阅读(234)  评论(0编辑  收藏  举报