JSP网络聊天室

1. 实现思路

1.     登录页面

在页面输入用户名密码,提交到登录逻辑页面

2.     登录逻辑页面

调数据库判断用户名密码的正确性

正确:跳到聊天室界面并且存储用户名

错误:返回登录界面

3.聊天室界面

   1.拿到存储的用户名

2.将发送的消息传到聊天室逻辑页面

3.拿到application中的值将application中的值展示到聊天记录文本域中

 

4.聊天室逻辑页面

将传过来的消息用application域对象存储起来

获取之前application中的值

2、逻辑思路图

 

3、实例代码:

Login.jsp  登录页面

<form action="loginOpreation.jsp" method="post">
用户名:<input type="text" name="uname" ><br/>
密码:<input type="password" name="upass" ><br/>
<input type="submit" value="登录" >
</form>

LoginOperation.jsp 登录逻辑页面

<!-- 登录逻辑页面 -->
 <%
 String uname=request.getParameter("uname");
 String upass=request.getParameter("upass");
 BuserDao bd=new BuserDao();
 Buser b=new Buser(uname,upass);
 if(bd.Login(b)){
	  session.setAttribute("uname", uname);
	  session.setAttribute("upass", upass);
	  request.getRequestDispatcher("chatRoom.jsp").forward(request, response);	 
 }else{
	 response.sendRedirect("login.jsp");
 }

chatRoom   聊天室

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- <meta http-equiv="refresh" content="5;url=chatRoom.jsp"> -->

<title>Insert title here</title>

<script type="text/javascript">
function $(o){
	return document.getElementById(o);
}
function s(){
	var str=$("input").value;
	if(str==""){
		alert("输入框不能为空,请输入!");
	}else{
		document.location="chatRoomOpreation.jsp?sendInfo="+str;;
	}
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
//获取application中的值
String all="";
Object o=application.getAttribute("all");
if(o!=null){
	all=o.toString();
	
}
%>

<h1>多人聊天室</h1>
你好:<%=session.getAttribute("uname") %>
<form  method="post">
<div style="width:1000px; height:350px; overflow:auto; border:1px solid #000000;">
<%=all
%>
</div>
<br/>
<!-- 第二种,输入框 -->
 <input type="text" name="sendInfo"  id="input"/> 

<!-- 第二种,富文本编辑器 -->
<!-- <textarea rows="10" cols="10" name="sendInfo" id="1"></textarea>
<script type="text/javascript">
         CKEDITOR.replace("sendInfo");
</script> -->
 <input type="button" value="发送" οnclick="s()">
</form>

</body>



chatRoomOpreation.jsp 聊天室逻辑页面

<!-- 聊天业务逻辑页面 -->
<%
request.setCharacterEncoding("UTF-8");
//获取用户名,用户名要拼接在发送的消息的前面
String uname=session.getAttribute("uname").toString();
//获取application中记录
String all="";
Object o=application.getAttribute("all");
if(o!=null){
	all=o.toString();
}
//1.获取要发送的内容
String sendInfo=request.getParameter("sendInfo").toString();
if(sendInfo==null){
	request.setAttribute("flage","YES");
	request.getRequestDispatcher("chatRoom.jsp").forward(request, response);
	//response.sendRedirect("chatRoom.jsp");
}
//将内容拼接
if("admin".equals(uname)){
	all=all+"<br/>"+uname+":<span style='color:red'>"+sendInfo+"</span>";
}
else if("sa".equals(uname)){
	all=all+"<br/>"+uname+":<span style='color:green'>"+sendInfo+"</span>";
}
//富文本编辑器内容拼接
  // all=all+"\r\n"+uname+":"+sendInfo;
//2.将获取的内容存到application中
application.setAttribute("all",all);
//3.跳转到chatRoom.jsp页面(重定向)
response.sendRedirect("chatRoom.jsp");


%>




  • 12
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值