最近在忙企业门户网站,我做的是在线帮助也可以说是在线聊天。以下是代码:用的是Java和JSP,数据库是SQLsever2005.有什么不对的地方;希望大家指正!
package help;
public class UserBean {
private String UserName;
private String UserPassword;
private String NickName;
private String Sex;
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserPassword() {
return UserPassword;
}
public void setUserPassword(String userPassword) {
UserPassword = userPassword;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public String getNickName() {
return NickName;
}
public void setNickName(String nickName) {
NickName = nickName;
}
}
}
package help;
import java.sql.*;
import java.sql.DriverManager;
public class ConnectDB {
//数据库用户名
String UserName="sa";
//数据库密码
String UserPassword="123";
//数据库的URL,包括连接数据库所使用的编码格式
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db.net";
//定义连接对象
Connection dbConn;
//错误信息串
String errMes;
public ConnectDB(){
//初始化操作
errMes="";
dbConn=null;
}
//连接数据库
public Connection getConn()
{
try{
//声明所用的类包
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//获取数据库的连接对象
dbConn=
DriverManager.getConnection(url,UserName,UserPassword);
}
catch(Exception e)
{
//dbConn = null;
errMes=e.toString();
}
return dbConn;
}
//获取错误信息
public String getErrMes()
{
return errMes;
}
}
package help;
import java.sql.*;
public class ExecuteDB extends ConnectDB {
//数据库连接对象
private Connection dbConn;
private Statement stmt;
private ResultSet rs;
//error描述错误信息
private String errMes;
//初始化操作
public ExecuteDB()
{
dbConn = super.getConn();
stmt = null;
rs =null;
this.errMes = super.getErrMes();
}
//执行SQL执行语句,主要是执行插入和删除的SQL语句
public boolean exeSql(String strSql){
boolean isSuc = false;
try
{
stmt=dbConn.createStatement();
stmt.executeUpdate(strSql);
stmt.close();
isSuc = true;
}
catch(Exception e){
this.errMes = this.errMes+"<br>"+e.toString();
}
return isSuc;
}
//执行sql查询语句
public ResultSet exeQuery(String strSql){
try
{
stmt= dbConn.createStatement();
rs= stmt.executeQuery(strSql);
}
catch(Exception e)
{
this.errMes = this.errMes +"<br>"+e.toString();
rs = null;
}
return rs;
}
//取得错误信息
public String getErrMes()
{
return errMes;
}
}
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.util.*" import="java.sql.*" %>
<%!
//定义变量
//String sUserName = null;
String sUserPassword = null;
String sSex = null;
String sNickName = null;
String strSql=null;
ResultSet rs = null;
%>
<html>
<head>
<title>欢迎光临在线帮助</title>
<jsp:useBean scope="page"id="ExecuteDB" class="help.ExecuteDB"/>
</head>
<body bgcolor="#dffdff">
<div align="center">
<h1>用户注册</h1>
<%
//设置编码格式
request.setCharacterEncoding("gb2312");
//获取注册的用户名
String sUserName=request.getParameter("UserName");
//以用户名为条件查询数据库
strSql = "select * from Users where UserName like'"+sUserName+"'";
rs = ExecuteDB.exeQuery(strSql);
//判断该用户名是否存在
if(!rs.next())
{//用户名不存在
//获取注册的其他信息
sUserPassword =request.getParameter("UserPassword");
sNickName =request.getParameter("NickName");
sSex =request.getParameter("Sex");
//String sSexs=trans(sSex);
//将注册信息保存到数据库中
strSql = "insert into Users(UserName,UserPassword,NickName,Sex)values('"+sUserName+"','"+sUserPassword+"','"+sNickName+"','"+sSex+"')";
if(ExecuteDB.exeSql(strSql))
{//注册信息保存成功
out.println("<p><font color=bule>注册成功</font></p>");
out.println("<p><input type=/"submit/"name=/"btn/"value=/"回登录页/" onClick=/"javascript:window.location='index.html'/"></p>");
}
else
{//注册信息保存失败
out.println("<p><font color=bule>注册失败</font></p>");
out.println("<p><font color=red>"+ExecuteDB.getErrMes()+"</font></p>");
out.println("<p><input type=/"submit/"name=/"btn/"value=/"返回/" onClick=/"javascript:window.location='index.html'/"></p>");
}
}
else
{//用户名已存在,给出提示信息
out.println("<p>用户名 <fontcolor=bule>"+sUserName+"</font> 已经存在</p>");
out.println("<p><input type=/"submit/"name=/"btn/"value=/"重新输入/" onClick=/"javascript:window.location='index.html'/"></p>");
}
rs.close();
%>
</div>
</body>
</html>
<%@ page contentType="text/html"language="java" import="java.util.*" pageEncoding="gb2312"%>
<%//获取当前用户的用户名
String sUserName = (String)session.getAttribute("UserName");
%>
<html>
<head>
<title>欢迎进入在线帮助</title>
</head>
<body bgcolor="#dffdff">
<h1 align="center">欢迎<font color="#ooooff">光临在线帮助</font></h1>
<hr>
<%
//获取所有信息
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>
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.util.*" import="java.sql.*" %>
<%!
//定义变量
int iUserNum = 0;
String sUserName = null;
String sSex = null;
String sNickName = null;
String sInfo=null;
%>
<html>
<head>
<meta http-equiv="refresh" content=“5”>
<title>欢迎光临在线帮助</title>
</head>
<body bgcolor="#dffdff">
<%
//获取Hashtable对象信息
Hashtable userList=(Hashtable)
application.getAttribute("userList");
Enumeration e = userList.elements();
//获取在线人数
iUserNum = userList.size();
%>
<div align= center>
[<A href="user_info.jsp"target="_self">刷新用户列表</A>]
</div>
<br>
在线人数:<font color="#ff0000"><%=iUserNum%></font> 人
<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="images/
<%if(sSex.equals("nv"))out.print("gg");else out.print("mm");%>.jpeg>">
<font color="#0000ff" size="2"><%= sNickName %></font><br>
<%
}
%>
</body>
</html>
<%@ page contentType="text/html" language="java" import="java.util.*" pageEncoding="gb2312"%>
<% //获取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("Frist","Not");
}
else
{
//判断用户是否刚进入在线帮助,如果是则将用户进入在线帮助的消息加入聊天信息中
if(isFirst == null)
{
String sWel ="<font color=red>"+sNickName+"</font>欢迎进入在线帮助! <br>";
vChat.add(sWel);
session.setAttribute("Frist","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_i"));
sAction = new String(sAction.getBytes("iso8859_i"));
sContent = new String(sContent.getBytes("iso8859_i"));
String sTotal="<font color=bule>"+ sNickName +"</font>"+
sAction+"对<font color=red>"+sTo+"</font>说:"+sContent+"<br>";
vChat.add(sTotal);
}
}
}%>
<html>
<head>
<title>在线帮助</title>
<body bycolor="#DFFDFF">
<BR>
<form name="form1" action="send_info.jsp" method="post" target="_self">
<%=sNickName%>
<input type="text" name="Content" size="30"
maxlength="50">
<input type="submit" name="sub" value="发言">
<a href="logout.jsp" target="_top">退出在线帮助</a>
<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>
</body>
</html>
<%@ page
contentType="text/html;char
set=gb2312" language="java"
import="java.util.*"
import="java.sql.*,help.*"%
>
<%!
//定义变量
String sUserName = null;
String sUserPassword1 =
null;
String sUserPassword2 =
null;
String sSex = null;
String sNickName = null;
String strSql=null;
ResultSet rs = null;
%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>欢迎光临在线帮
助</title>
<jsp:useBean
scope="page"id="ExecuteDB"
class="help.ExecuteDB"/>
</head>
<%
//获取登陆的用户名和
密码
sUserName =
request.getParameter
("UserName");
sUserPassword1=
request.getParameter
("UserPassword");
sUserPassword2=
request.getParameter
("UserPassword");
//以用户名为条件查询数据库,
以便判断该用户名是否存在
strSql = "select * from
Users where UserName
like'"+sUserName+"'";
rs = ExecuteDB.exeQuery
(strSql);
if(rs.next())
{//登陆的用户名存在
// 获取该用户名在数据库
中对应的其他信息
sUserPassword1 =
rs.getString
("UserPassword");
sSex = rs.getString
("Sex");
sNickName = rs.getString
("NickName");
//判断登陆密码是否正确
if(sUserPassword1.equals
(sUserPassword2 ))
{
//将用户名保存到session当
中
session.setAttribute
("UserName",sUserName);
session.setAttribute
("Sex",sSex);
session.setAttribute
("NickName",sNickName);
//构建一个Hashtable对象,
以存储在线用户信息
Hashtable userList=
(Hashtable)
application.getAttribute
("userList");
if(userList == null)
{
userList = new Hashtable
();
}
//将当前登陆用户信息添加到
在线用户列表中
userList.put
(sUserName,sUserName
+"**"+sSex
+"***"+sNickName);
application.setAttribute
("userList",userList);
%>
<!--显示框架页-->
<frameset cols="180,*">
<frame
src="user_info.jsp"
name="User" noresize
frameborder=0>
<frameset rows="*,120">
<frame
src="display_info.jsp"
name="Display" noresize
frameborder=0>
<frame
src="send_info.jsp"
name="Send" noresize
frameborder=0>
</frameset>
</frameset>
<onframes>
<BODY bgcolor="#dffdff">
<h1>浏览器要求支持框架页
</h1>
</BODY>
</onframes>
<%
}
else{
//登录密码错误
out.println("<body
bgcolor=/"#dffdff/">");
out.println("<p
align=center><font
color=bule>用户密码错误
</font></p>");
out.println("<p
align=center><input type=
/"button/"name=/"btn
/"value=/"重新登录
/"onClick=
/"javascript:window.history
.go(-1)/"></p>");
out.println("</body>");
}
}
else{
//登录的用户名错误
out.println("<body
bgcolor=/"#dffdff/">");
out.println("<p
align=center>用户名
<font color=bule>"+
sUserName + "</font>不存在
</p>");
out.println("<p
align=center><input type=
/"button/"name=/"btn
/"value=/"重新登录/"
onClick=
/"javascript:window.history
.go(-1)/"></p>");
out.println("</body>");
}
%>
</html>
<%@ page contentType="text/html;charset=gb2312"language="java" import="java.util.*" import="java.sql.*,help.*"%>
<html>
<head>
<title>欢迎进入在线帮助</title>
</head>
<%
//获取当前用户信息
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>"+sNickName+"</font>离开了聊天室!<br>";
vChat.add(slogout);
//使当前session失效
session.invalidate();
%>
<body bgcolor="#dffdff">
<h1 align="center">欢迎下次光临</h1>
<p align="center"><input type="button" name="btn" value="关闭窗口" onClick="javascript:window.close()"></p>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>user_reg_form.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body><div align="center"><strong><font size="6">用户注册</font></strong>
<br></div><div align="center"><br>
<form method="post" action="user_reg_save.jsp" name="zhuce">
<p>用户名:<input type="text" name="UserName"> <br></p>密码:<input type="password" name="UserPassword">
<br><br>昵称:<input type="text" name="NickName"><br>
<br>性别:<input type="radio" value="nan" name="Sex">男<input type="radio" value="nv" name="Sex">女
<br><br><br><br>
<br><input type="submit" value="注册" name="button5"><input type="reset" value="重置" name="button6">
<br><p> </p><p> </p><p> </p><p> </p></form><br><br>
</div></body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>欢迎光临在线帮助</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<div align="center">
<h1>欢迎观临在线帮助</h1>
<br><form method="post" action="main.jsp" name="index"><br>
<p>用户名:<input type="text" name="UserName"></p>
<p><br>密码:<input type="password" name="UserPassword"> </p>
<p> </p>
<p><input type="submit" value="登录" name="button1">
<input type="reset" value="重置" name="button2"> <a href="user_reg_form.html">注册</a> </p>
<p> </p>
<p> </p><p> </p></form><br>
</body>
</html>