ConnectDB.java(连接MySQL数据库)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
package ss.tmm;
import java.sql.*;
import java.io.*;
public class ConnectDB
{
private Connection con = null;
private final String DBDRIVER = "com.mysql.jdbc.Driver";
private final String DBURL = "jdbc:mysql://localhost:3306/web_chatting";
private String DBUSER = "root";
private String DBPASSWORD = "root";
//获取数据库的链接方法
public Connection getConnect()
{
try
{
Class.forName(DBDRIVER);
con = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
}
catch(Exception ex)
{
ex.printStackTrace();
}
return con;
}
}
ExecuteDB.java(执行数据库的查找和修改)
package ss.tmm;
import java.sql.*;
import java.io.*;
public class ExecuteDB extends ConnectDB
{
private Connection dbcon;
private Statement stmt;
private ResultSet rs;
//主要是执行插入和删除的sql语句
public boolean exeSql(String strSql)
{
boolean isSuc = false;
try
{
dbcon = super.getConnect();
stmt = dbcon.createStatement();
stmt.executeUpdate(strSql);
stmt.close();
isSuc = true;
}
catch(Exception ex)
{
System.out.println(ex);
}
return isSuc;
}
//执行sql查询语句
public ResultSet exeQuery(String strSql)
{
try
{
dbcon = super.getConnect();
stmt = dbcon.createStatement();
rs = stmt.executeQuery(strSql);
}
catch(Exception ex)
{
System.out.println(ex);
}
return rs;
}
}
UserBean.java(与数据表user相关的JavaBean文件)
package ss.tmm;
public class UserBean
{
private int userID;
private String userName;
private String userPassword;
private String sex;
private String nickName;
public UserBean()
{
}
public void setUserName(String userName)
{
this.userName = userName;
}
public void setUserPassword(String userPassword)
{
this.userPassword = userPassword;
}
public void setSex(String sex)
{
this.sex = sex;
}
public void setNickName(String nickName)
{
this.nickName = nickName;
}
public int getUserID()
{
return this.userID;
}
public String getUserName()
{
return this.userName;
}
public void getUserPassword()
{
return this.userPassword;
}
public String getSex()
{
return this.sex;
}
public String getNickName()
{
return this.nickName;
}
}
index.html(聊天室首页,提供用户登录与注册的接口)
<html>
<head>
<title>欢迎光临红尘遗梦的聊天室 </title>
</head>
<body background=" index.jpg">
<center><br><br>
<marquee behavior="alternate" direction="right" scrolldelay="80"><font color="red" size="6">欢迎光临红尘遗梦的聊天室,谢谢!</font></marquee><br><br>
<font color="green" size="5">红尘遗梦聊天室</font><br><br>
<form action="main.jsp" method="post">
<table border="1">
<tr><td align="right">用户名:</td><td><input type="text" name="username"></td></tr>
<tr><td align="right">密 码:</td><td><input type="password" name="userpassword"></td></tr>
<tr><td align="center" colspan="2"><input type="submit" value="登录"> <input type="reset" value="重填"> <a href="user_register.html">注册</a></td></tr>
</table>
</form>
</center>
</body>
</html>
user_register_form.html(用户注册页面)
<html>
<head>
<title>聊天室用户注册</title>
</head>
<body background=" register.jpg">
<center>
<font color="red" size="5">用户注册</font><br><br>
<form action="user_register_save.jsp" method="post">
<table border="1">
<tr><td align="right">用户名:</td><td><input type="text" name="username"></td></tr>
<tr><td align="right">密 码:</td><td><input type="password" name="userpassword1"></td></tr>
<tr><td align="right">确认密码:</td><td><input type="password" name="userpassword2"></td></tr>
<tr><td align="right">昵 称:</td><td><input type="text" name="nickname"></td></tr>
<tr><td align="right">性 别:</td><td align="center"><input type="radio" name="sex" value="male" checked>男 <input type="radio" name="sex" value="female">女</td></tr>
<tr><td align="center" colspan="2"><input type="submit" value="注册"> <input type="reset" value="重填"></td></tr>
</table>
</form>
</center>
</body>
</html>
user_register_save.jsp(处理注册信息页面)
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%
request.setCharacterEncoding("gb2312");
%>
<jsp:useBean id="executeDB" scope="page" class="ss.tmm.ExecuteDB"/>
<html>
<head>
<title>用户注册信息验证和保存</title>
</head>
<body>
<center>
<%!
ResultSet rs = null;
%>
<%
request.setCharacterEncoding("gb2312");
//接收来自表单的数据
String name = request.getParameter("username");
System.out.println(name);
String sql = "select * from user where userName like '"+name+"'";
rs = executeDB.exeQuery(sql);
if(!rs.next())
{
//用户名不存在获取其他信息
String password01 = request.getParameter("userpassword1");
String password02 = request.getParameter("userpassword2");
String nick = request.getParameter("nickname");
String sex_id = request.getParameter("sex");
//将信息写入数据表中
String strSQL = "insert into user(userName,userPassword,sex,nickName) values('"+name+"','"+password01+"','"+sex_id+"','"+nick+"')";
if(executeDB.exeSql(strSQL))
{
//注册信息保存成功
out.println("<p><font color=red>注册成功</font></p>");
out.println("<p><input type=/"button/" name=/"btn/" value=/"回登录页/" onClick=/"javascript:window:location='index.html'/"></p>");
}
else
{
//注册信息保存失败
out.println("<p><font color=red>注册失败</font></p>");
out.println("<p><input type=/"button/" name=/"btn/" value=/"返回/" onClick=/"javascript:window.history.go(-1)/"></p>");
}
}
else
{
//用户名已经存在给出错误提示
//注册信息保存成功
out.println("<p>用户:<font color=red>"+name+"</font>已经存在!</p>");
out.println("<p><input type=/"button/" name=/"btn/" value=/"重新输入/" onClick=/"javascript:window.history.go(-1)/"></p>");
}
rs.close();
%>
</center>
</body>
</html>
user_info.jsp(显示在线用户信息页面)
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%
request.setCharacterEncoding("gb2312");
%>
<%!
int sUserNum = 0;
String susername = null;
String snickname = null;
String ssex = null;
String sinfo = null;
%>
<html>
<head>
<title>欢迎光临红尘遗梦的聊天室</title>
</head>
<body>
<center>
<%
//获取hashtable对象的信息
Hashtable userlist = (Hashtable)application.getAttribute("userlist");
Enumeration e = userlist.elements();
//获取在线人数
sUserNum = userlist.size();
%>
<div align="center">[<a href="user_info.jsp" target="_self">刷新用户列表</a>]
</div>
<br>在线人数:<font color="red" size="4"><%=sUserNum%></font><br><hr>
<%
//循环显示所有在线用户的信息
while(e.hasMoreElements())
{
//获取某个用户的信息
sinfo = (String)e.nextElement();
//获取用户名
int i = sinfo.indexOf("**");
if(i!=-1)
{
susername = sinfo.substring(0,i);
sinfo = sinfo.substring(i+2);
}
//获取性别
i = sinfo.indexOf("***");
if(i!=-1)
{
ssex = sinfo.substring(0,i);
sinfo = sinfo.substring(i+3);
}
//获取昵称
snickname = sinfo;
%>
<!--根据性别显示相应的图片-->
<img src="<%
if(ssex.equals("male")) out.println("boy");else out.println("girl");%>.ico"/>
<font color="green" size="4"><%=snickname%></font><br>
<%
}
%>
</center>
</body>
</html>
send_info.jsp(发送聊天信息页面,同时具有处理聊天信息的功能)
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%
//获取session中相应变量的值
String isFirst = (String)session.getAttribute("First");
String sNickName = (String)session.getAttribute("nickName");
//获取保存在application中的聊天信息
Vector vChat = (Vector)application.getAttribute("vChat");
if(vChat==null)
{
vChat = new Vector();
application.setAttribute("vChat",vChat);
String sWel = "<font color=red>"+sNickName+"</font>进入了聊天室<br>";
vChat.add(sWel);
session.setAttribute("First","Not");
}
else
{
//判断用户是否是刚进入聊天室,如果是则将用户进入聊天室的消息加入聊天信息中
if(isFirst==null)
{
String sWel = "<font color=red>"+sNickName+"</font> 进入到聊天室!<br>";
vChat.add(sWel);
session.setAttribute("First","Not");
}
else
{
//获取聊天信息
String scontent = request.getParameter("content");
String sto = request.getParameter("to");
String saction = request.getParameter("action");
//判断聊天内容是否为空,如果不为空,就将聊天内容放到Vector对象中去
if(scontent!=null&&scontent!="")
{
//编码转换
sto = new String(sto.getBytes("iso8859_1"));
saction = new String(saction.getBytes("iso8859_1"));
scontent = new String(scontent.getBytes("iso8859_1"));
String sTotal = "<font color=red>"+sNickName+"</font>"+"<font color=green> "+saction+"</font>"+" 对<font color=blue> "+sto+"</font>说: "+ "<font color=auqa>"+scontent+"</font><br>";
vChat.add(sTotal);
}
}
}
%>
<html>
<head>
<meta http-equiv=refresh content="5">
<title>欢迎光临红尘遗梦的聊天室</title>
</head>
<body>
<center>
<form action="send_info.jsp" method="post" target="_self">
<%=sNickName%>
<input type="text" name="content" size="30" maxlength="50">
<input type="submit" value="发言"> <a href="logout.jsp" target="_top">退出聊天室</a>
<br><br><br>
对
<select name="to"><option value="所有人" selected>所有人</option>
<%
//获取当前在线的用户信息
Hashtable userlist = (Hashtable)application.getAttribute("userlist");
Enumeration e = userlist.elements();
while(e.hasMoreElements())
{
String sinfo = (String)e.nextElement();
int i = sinfo.indexOf ("***");
if(i!=-1)
{
sinfo = sinfo.substring(i+3);
}
out.println("<option value=/""+sinfo+"/">"+sinfo+"</option>");
}
%>
</select>
动作表情
<select name="action">
<option value="" selected>无表情</option>
<option value="微笑着">微笑</option>
<option value="脸红红的">脸红</option>
<option value="凶狠狠的">凶恶</option>
<option value="依依不舍的">告别</option>
<option value="热情的">打招呼</option>
</select>
</form>
</center>
</body>
</html>
show_info.jsp(显示聊天信息页面)
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%
//获取当前用户的用户名
String susername = (String)session.getAttribute("userName");
%>
<html>
<head>
<meta http-equiv=refresh content="5">
<title>欢迎光临红尘遗梦的聊天室</title>
</head>
<body background=" main.jpg">
<center>欢迎<font color="red" size="5"><%=susername%></font>光临红尘遗梦的聊天室</center><br><br>
<%
Vector vChat = (Vector)application.getAttribute("vChat");
if(vChat!=null)
{
Object objWords[] = vChat.toArray();
int ilen = objWords.length;
for(int i=ilen-1;i>=0;i--)
{
out.println(objWords[i]);
}
}
%>
</body>
</html>
main.jsp(聊天室主页面,同时处理用户的登录信息)
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%
request.setCharacterEncoding("gb2312");
%>
<jsp:useBean id="executeDB" scope="page" class="ss.tmm.ExecuteDB"/>
<%!
String sname = null;
String spassword1 = null;
String spassword2 = null;
String ssex = null;
String snickname = null;
String strSql = null;
ResultSet rs = null;
%>
<html>
<head>
<meta http-equiv=refresh content="5">
<title>欢迎光临红尘遗梦的聊天室</title>
</head>
<!--显示框架页-->
<frameset cols="210,*">
<frame src="user_info.jsp" name="user" noresize frameborder=0>
<frameset rows="*,120">
<frame src="show_info.jsp" name="display" noresize frameborder=0>
<frame src="send_info.jsp" name="send" noresize frameborder=0>
</frameset>
</frameset>
<noframes>
<body bgcolor="red">
<h2>浏览器要求支持框架集</h2>
</body>
</noframes>
<body background=" main.jpg">
<center>
<%
//获取登录的用户名和密码
sname = request.getParameter("username");
spassword1 = request.getParameter("userpassword");
//以用户名为条件查询数据库,判断该用户是否存在
strSql = "select * from user where userName like '"+sname+"'";
rs = executeDB.exeQuery(strSql);
if(rs.next())
{
//用户名存在,获取用户的其他信息
spassword2 = rs.getString("userPassword");
ssex = rs.getString("sex");
snickname = rs.getString("nickName");
//判断登录密码是否正确
if(spassword1.equals(spassword2))
{
//将用户信息保存带session中
session.setAttribute("userName",sname);
session.setAttribute("sex",ssex);
session.setAttribute("nickName",snickname);
//构建一个hashtable对象,用以存储在线用户
Hashtable userlist = (Hashtable)application.getAttribute("userlist");
if(userlist==null)
{
userlist = new Hashtable();
}
userlist.put(sname,sname+"**"+ssex+"***"+snickname);
application.setAttribute("userlist",userlist);
}
else
{
//登录密码错误
out.println("<p align=center><font color=red>用户密码错误</font></p>");
out.println("<p align=center><input type=/"button/" name=/"btn/" value=/"重新登录/" onClick=/"javascript:window.history.go(-1)/"></p>");
}
}
else
{
//用户名错误
out.println("<p align=center><font color=red>用户名不存在</font></p>");
out.println("<p align=center><input type=/"button/" name=/"btn/" value=/"重新登录/" onClick=/"javascript:window.history.go(-1)/"></p>");
}
%>
</center>
</body>
</html>
logout.jsp(用户退出聊天室)
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%
request.setCharacterEncoding("gb2312");
%>
<html>
<head>
<title>欢迎光临红尘遗梦的网上聊天系统</title>
</head>
<body background=" main.jpg">
<center>
<%
//获取当前用户的信息
String susername = (String)session.getAttribute("userName");
String snickname = (String)session.getAttribute("nickName");
//获取在线用户信息
Hashtable userlist = (Hashtable)application.getAttribute("userlist");
//在线用户列表中删除该当前用户
userlist.remove(susername);
//更新application中的列表
application.setAttribute("userList",userlist);
//发送消息说明当前用户已经离开聊天室
Vector vChat = (Vector)application.getAttribute("vChat");
String slogout = "<font color=red size=5>"+snickname+"</font>已经离开聊天室!<br>";
vChat.add(slogout);
//使当前的session失效
session.invalidate();
%>
<font color="blue" size="5">欢迎下次光临红尘遗梦的聊天室</font><br><br>
<input type="button" value="退出聊天室" onClick="javascript:window.close()">
</center>
</body>
</html>