JSP实现简易的聊天功能(Session机制)
基于服务器端全局应用空间 application 的简易聊天室实现
1.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>聊天室登录</title>
</head>
<style>
.div1{
background-color:skyblue;
border:solid 1px black;
width:300px;
height:170px;
text-align:center;
line-height:150%
}
</style>
<body>
<div class="div1">
<%
StringBuffer view=new StringBuffer("");
session.setAttribute("view",view);
String color="balck";
session.setAttribute("Color",color);
%>
<h3>聊天室登录</h3>
<form action="cotainer.jsp" method="post">
用户名:<input type="text" name="userName"><br>
<br>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>
2.container.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<style>
.div3{
background-color:lightgreen;
border:solid 1px black;
width:700px;
height:600px;
line-height:150%
}
img{
width:15px;
height:20px;
}
</style>
</head>
<body>
<div class="div3">
<%
request.setCharacterEncoding("utf-8");
if(request.getParameter("userName")!=null)
{
session.setAttribute("userName",request.getParameter("userName").toString());}
String sessionID=session.getId();
StringBuffer users=new StringBuffer();
//确定用户数量
if(application.getAttribute("count")!=null)
{
users=users.append(application.getAttribute("users").toString());
if(users.indexOf(sessionID)==-1){
session.setAttribute("isNew","true");
int userCount= Integer.parseInt(application.getAttribute("count").toString());
userCount++;
application.setAttribute("count",new Integer(userCount));
users=users.append("<br>"+sessionID);
application.setAttribute("users",users.toString());
}
}
else
{
users=users.append("<br>"+sessionID);
application.setAttribute("users", users.toString());
application.setAttribute("count", "1");
}
//获取默认颜色
String select="black";
if(request.getParameter("Color")!=null)
select=request.getParameter("Color").toString();
request.setAttribute("select",select);
//确定用户次序
int userId=1;
String[]userss=users.toString().split("<br>");
for(int i=0;i<userss.length;i++){
if(userss[i].equals(sessionID)){
userId=i;
break;
}
}
%>
您是第<img src="数字/<%=userId%>.png">位访问者!
<jsp:include page="view.jsp" />
<form action="cotainer.jsp" method="post">
<input type="text" name="message">
<input type="hidden" name="send" value="true">
选择我的颜色:<select name="color">
<option value="red" ${
select=="red"?'selected':''}>红色</option>
<option value="blue" ${
select=="blue"?'selected':''}>蓝色</option>
<option value="green" ${
select=="green"?'selected':''}>绿色</option>
<option value="black" ${
select=="black"?'selected':''}>黑色</option>
<option value="yellow" ${
select=="yellow"?'selected':''}>黄色</option>
<option value="purpole" ${
select=="purpole"?'selected':''}>紫色</option>
</select>
<input type="submit" value="发送">
</form>
<form action="cotainer.jsp">
<input type="hidden" name="clear" value="true">
<input type="submit" value="清空"></form>
<form action="cotainer.jsp">
<input type="hidden" name="flush" value="true">
<input type="submit" value="刷新"></form>
<form action="login.jsp">
<input type="submit" value="退出聊天">
</form>
</div>
</body>
</html>
view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*" import="java.text.SimpleDateFormat"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>聊天</title>
<style>
.div2{
border:solid 1px black;
width:500px;
height:400px;
overflow-y:scroll;
}</style>
</head>
<body>
<%request.setCharacterEncoding("utf-8");
String userName = session.getAttribute("userName").toString();
if(request.getParameter("color")!=null)
session.setAttribute("Color",request.getParameter("color").toString());
String color=session.getAttribute("Color").toString();
request.setAttribute("select",color);
StringBuffer view=new StringBuffer();
view.append(session.getAttribute("view").toString());
String chatTexts="";
//判断是不是新用户
String isNew="false";
if(session.getAttribute("isNew")!=null){
isNew=session.getAttribute("isNew").toString();
session.setAttribute("isNew","false");
}
if(isNew.equals("false"))
{
//发送
String send="false";
if (request.getParameter("send") != null)
send = "true";
if(send.equals("true")){
if (request.getParameter("message") != null) {
String message = request.getParameter("message").toString();
Date time = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time1 = sdf.format(time);
String chatText ="<br>"+ userName + "["+time1 + "]:" + message;
view.append(chatText);
session.setAttribute("view",view.toString());
//view存到application中
if (application.getAttribute("chatText") != null) {
String ss=application.getAttribute("chatText").toString();
StringBuffer ssb=new StringBuffer();
ssb.append(ss);
ssb.append(chatText);
application.setAttribute("chatText",ssb.toString());
}
else{
application.setAttribute("chatText",view.toString());
}
}
}
//清空
String clear="false";
if (request.getParameter("clear") != null)
clear = "true";
if(clear.equals("true")){
view.delete(0,view.length());
}
session.setAttribute("view",view.toString());
//刷新
String flush="false";
if (request.getParameter("flush") != null)
flush ="true";
if (flush.equals("true")) {
if (application.getAttribute("chatText") != null) {
chatTexts=application.getAttribute("chatText").toString();
view.delete(0,view.length());
view.append(chatTexts);
session.setAttribute("view",view.toString());
}
}
//加颜色
String scolor=view.toString();
String[]a=scolor.split("<br>");
for(int i=0;i<a.length;i++){
if(a[i].contains((userName+"["))){
if(a[i].contains("<span")){
//问题一:字符串取子串
a[i]=a[i].substring(0,a[i].length()-7);
int index=a[i].lastIndexOf(">");
a[i]=a[i].substring(index+1);
}
a[i]="<br><span style='color:"+color+"'>"+a[i]+"</span>";
}
else{
a[i]="<br>"+a[i];
}
}
StringBuffer sb=new StringBuffer();
for(String s:a){
sb.append(s);
}
view.delete(0,view.length());
view.append(sb.toString());
}
%>
<div class="div2">
<%=view.toString() %>
</div>
</body>
</html>
通过切换不同的浏览器可以实现在本机进行聊天。
(2)聊天室网站建好后,若尝试和同学一块开展聊天,你分享给他人的网站地址的构成应该是什么?
网址的构成:http://localhost:8080/项目名/...
(3)如果该网站只在本机能够使用,而其他同学无法访问,请分析原因是什么?应如何解决这个问题?
运行的项目不再同一局域网内。
解决办法:
一般我们想在别人电脑上运行本机tomcat项目,不考虑服务器的话,必须在同一局域网中,而网线连接和无线网络连接之间还是有很多不同的,流程基本都是在本机将项目运行起来,然后找到本机ip,将http://localhost:8080/项目名/...中的localhost换成本机ip(注意:必须在同一局域网中)。