应用session和application对象设计聊天室

来源:互联网 发布:更改歌曲属性数据无效 编辑:程序博客网 时间:2024/05/22 07:41

1、用户只需输入一个用户名就可以进入聊天室,但是如果当前有人在使用该用户名,那么就必须换一个唯一的用户名。

2、 进入聊天室后,用户可以从用户信息窗口看到该聊天室中所有用户的用户名,也可以在聊天窗口中看到随时更新的聊天信息。但用户登录之前的信息不能看到。用户可以给所有人或某一个聊天用户发送公共的聊天信息,这个聊天内容大家都可以看到。用户也可以给某个用户发送私人的聊天信息,这种信息属于私聊信息,只有发送者和接收者可以看到。此外,聊天窗口还会出现一些系统公告,比如某某上站、某某离开等消息,另外用户还可以自己定义聊天信息和聊天用户信息刷新的时间间隔。

3、  在用户单击“退出”按钮后,页面关闭,同时application和session中保存的信息都将丢失。

第一个界面,用框架布局整个聊天界面index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<frameset cols="15%,85%" rows="*" border="1">
   <frame src="sy4-xs.jsp"/>
     <frameset cols="*" rows="55%,45%">
        <frame src="sy4-lt.jsp" name="liaotian"/>
        <frameset cols="*" rows="50%,50%">
           <frame src="sy4-sl.jsp"/>
           <frame src="sy4-xxi.jsp"/>
        </frameset>
   </frameset>
</frameset>
</head>
<body>
</body>
</html>

第二个页面,用户登录sy4-dl.jsp,登录界面如下


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<style>
.mydiv{ 
   width:300px;  
   height:200px;  
   position:absolute;  
   left:50%;  
   top:50%;  
   margin:-100px 0 0 -150px 

</style>
<body background="6.gif" 
style="background-repeat: no-repeat; background-position: center top;">
<div class="mydiv">
<form action="sy4-dlcl.jsp" >
<table>
<tr><td align="center"><h2>欢迎进入聊天室</h2></td></tr>
<tr><td>  用户名:<input type="text" name="name"></td></tr>
<tr><td align="center">  <input type="submit" value="登录" ></td></tr>
  </table>
</form>
</div>
</body>
</html>

第三个界面,登录处理界面sy4-dlcl.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%! List<String> list=new ArrayList<String>();%>
   <%  
      Date data=new Date();
      SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String dltime=format.format(data);
      String str=request.getParameter("name"); 
      session.setAttribute("myname", str);
      session.setAttribute("time", dltime);
      application.removeAttribute("yhm");
   application.setAttribute("yhm",str+"上线!");
     for(int i=0;i<list.size();i++){
    if(str.equals(list.get(i))||str==""){%>
    <jsp:forward page="sy4-dl.jsp"></jsp:forward>
<%     }
     }
   list.add(str);
   application.setAttribute("yh", list);
   request.getRequestDispatcher("index.jsp").forward(request, response);
%>
</body>
</html>

第四个界面,在线用户显示界面

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="#00FFFF">
<h2 align="center">在线用户</h2>
<div align="center">
<%
List<String> list = (List) application.getAttribute("yh");
for (int i = 0; i < list.size(); i++) {
%>
<%=list.get(i)%>
<br>
<%
}
response.setHeader("refresh", "5");
%>
</div>
</body>
</html>

第五个界面,消息发送界面sy4-xxi.jsp(本刷新来准备用ajax实现异步刷新,但是初学者还不熟悉,只能用response.setHeader("refresh","5")实现刷新)

<%@page import="javax.xml.ws.Response"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="JS/jquery-2.2.3.min.js"></script>
</head>
<script type="text/javascript">
 /*  $(document).ready(function () {
$("#send").click(function () {
if($("#xinxi").val==""){
alert("不能发送空信息!");
$("#xinxi").focus();
}
$.post("sy4-xxicl.jsp?action=send",{
xinxi:$("#xinxi").val(),
lt:$("#sl").val(),
lt:$("#ql").val(),
name:$("#dx").val()
});
$("#xinxi").val("");
$("#xinxi").focus;
});
});
   */
  /* function createRequest(url) {
http_request=false;
if(window.XMLHttpRequest){
http_request=new XMLHttpRequest();
}else if(window.ActiveXObject){
try {
http_request=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request=new  ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
}
}
if(!http_request){
alert("不能创建XMLHttpRequest对象实例!");
return false;
}
http_request.onreadystatechange=function(){

};
http_request.open('GET',url,true);
http_request.send(null);
}


   function shuaxin() {
createRequest('sy4-lt.jsp');
createRequest('sy4-sl.jsp');
}
   
   function shuax() {
createRequest('sy4-lt.jsp');
createRequest('sy4-xs.jsp');
}
   */
   function qkong() {
form1.submit();
form1.reset();
}
   function shuax() {
  window.location.reload();
}
   
</script>


<body>
<form action="sy4-xxicl.jsp" method="post" target="liaotian" name="form1" >
<textarea rows="6" cols="120" name="xinxi" id="xinxi"></textarea>
<br> <input type="radio" name="lt" value="siliao" id="sl">私聊 
<select name="name" id="dx">
<option selected="selected" ></option>
<%
List<String> list = (List) application.getAttribute("yh");
 
for (int i = 0; i < list.size(); i++) {
%>
<option><%=list.get(i)%></option>
<%
}
%>
</select> 
<input type="radio" name="lt" value="qunliao" checked="checked" id="ql">群聊
<input type="button" name="fs" value="发送" align="left" id="send" onclick="qkong();">
<input type="reset" name="cz" value="刷新/重置" onclick="shuax();">
<a href="tuichu.jsp?name1=<%=session.getAttribute("myname").toString()%>"
target="_parent" onclick="shuax();">退出</a>
</form>

</body>
</html>

第六个界面,消息处理界面sy4-xxicl.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!List<String> list = new ArrayList<String>();
List<String> list1 = new ArrayList<String>();
%>
<%
String name = session.getAttribute("myname").toString();
try {
Date data = new Date();
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String time1 = format.format(data);
String liaotian = request.getParameter("lt");
if (liaotian.equals("siliao")) {
String yhm = request.getParameter("name");
list1.add(name + "#对#" + yhm + "#说:"
+ request.getParameter("xinxi") + "&nbsp;&nbsp;"+ time1);
} else {
list.add(name + ":#" + request.getParameter("xinxi") + "#"
+ time1);


}
} catch (Exception e) {


}
application.setAttribute("liao", list1);
application.setAttribute("list", list);
response.sendRedirect("sy4-lt.jsp");
%>
</body>
</html>

第七个界面,群聊显示界面sy4-lt.jsp

<%@page import="java.util.ArrayList"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<style>


</style>
<body>
<div style="overflow-y:auto; overflow-x: auto; width: 100%; height: 400px; ">
<%
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
%>
<div align="center" style="color: blue;">
<%=application.getAttribute("yhm").toString()%>
</div>
<%
response.setHeader("refresh", "5");
List<String> list = (List) application.getAttribute("list");
String timenow = session.getAttribute("time").toString();
Date time = format.parse(timenow);
if (list == null) {
out.print("欢迎来到聊天室!");
} else {
for (int i = 0; i < list.size(); i++) {
String[] sp = list.get(i).split("#");
Date time1 = format.parse(sp[2]);
if (time.getTime() <= time1.getTime()) {%>
   <%= sp[0]+ "&nbsp;" %>
<input type="text" value="<%= sp[1]%>" style="background-color: pink;">
<%= "&nbsp;"+ format.format(time1) %>
<br>
<%
}
}
}
%>
</div>
</body>
</html>

第八个界面,私聊显示界面,sy4-sl.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%try{
response.setHeader("refresh", "5");
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");


List<String> list1 = (List<String>) application.getAttribute("liao");
if (list1 == null || list1.isEmpty()) {


} else {
String name = session.getAttribute("myname").toString();
for (int i = 0; i < list1.size(); i++) {
String[] sp = list1.get(i).split("#");
if (sp[0].equals(name)||sp[2].equals(name)) {
out.print(sp[0] + sp[1] + sp[2]+sp[3]);
%>
<br>
<%
}
}
}
}catch(Exception e){

}
%>
</body>
</html>

第九个页面,退出后跳转页面tuichu.jsp

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功退出!
<%
String name1 = request.getParameter("name1");
Object name = (Object) name1;
List<String> list = (List) application.getAttribute("yh");
list.remove(name);
application.setAttribute("yhm", name + "离开聊天室!");
%>
<a href="sy4-dl.jsp">重新登录</a>
</body>
</html>

最后实现页面(界面有点简单),左边是在线用户,右边的上面是群聊,下面是私聊,最后下面是聊天输入界面(当消息发送出去后,聊天信息被清空)


0 0
原创粉丝点击