基于javaweb+mysql的jsp+servlet聊天室(java+jsp+servlet+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
登录、注册、退出、聊天
技术框架
CSS+JavaScript+ajax+jsp Servlet jdbc MySQL
基于javaweb+mysql的JSP+Servlet聊天室(java+jsp+servlet+mysql)
session.setAttribute("username", username); //保存当前登录的用户名
session.setAttribute("loginTime", new Date().toLocaleString()); //保存登录时间
ServletContext application = getServletContext();
String sourceMessage = "";
if (null != application.getAttribute("message")) {
sourceMessage = application.getAttribute("message").toString();
}
sourceMessage += "系统公告:<font color='gray'>" + username + "走进了聊天室!</font><br>";
application.setAttribute("message", sourceMessage);
try {
request.getRequestDispatcher("login_ok.jsp").forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// 发送聊天信息
public void sendMessages(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Random random = new Random();
String from = request.getParameter("from"); //发言人
String face = request.getParameter("face"); //表情
String to = request.getParameter("to"); //接收者
String color = request.getParameter("color"); //字体颜色
String content = request.getParameter("content"); //发言内容
String sendTime = new Date().toLocaleString(); //发言时间
ServletContext application = getServletContext();
String sourceMessage = application.getAttribute("message").toString();
try {
//发言时间
sourceMessage += "<font color='blue'><strong>" + from + "</strong></font><font color='#CC0000'>" + face + "</font>对<font color='green'>[" + to + "]</font>说:" + "<font color='" + color + "'>" + content + "</font>(" + sendTime + ")<br>";
application.setAttribute("message", sourceMessage);
request.getRequestDispatcher("Messages?action=getMessages&nocache=" + random.nextInt(10000)).forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
public class Messages extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String action = request.getParameter("action");
if ("loginRoom".equals(action)) { //登录时,写入系统公告
this.loginRoom(request, response);
} else if ("sendMessage".equals(action)) { //发送聊天信息
this.sendMessages(request, response);
} else if ("getMessages".equals(action)) { //从XML文件中读取聊天信息
this.getMessages(request, response);
}
}
// 将页面重定向到显示聊天信息的页面
public void getMessages(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
try {
request.getRequestDispatcher("content.jsp").forward(request, response);
} catch (Exception ex) {
System.out.println("vector" + i + ":" + vector.elementAt(i) + " user:" + username);
if (username.equals(vector.elementAt(i))) {
PrintWriter out;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
flag = false;
break;
}
}
}
//保存用户信息
if (flag) {
UserListener ul = new UserListener(); //创建UserListener的对象
ul.setUser(username); //添加用户
user.addUser(ul.getUser()); //添加用户到UserInfo类的对象中
session.setAttribute("user", ul); //将UserListener对象绑定到Session中
session.setAttribute("username", username); //保存当前登录的用户名
session.setAttribute("loginTime", new Date().toLocaleString()); //保存登录时间
ServletContext application = getServletContext();
String sourceMessage = "";
if (null != application.getAttribute("message")) {
sourceMessage = application.getAttribute("message").toString();
}
sourceMessage += "系统公告:<font color='gray'>" + username + "走进了聊天室!</font><br>";
application.setAttribute("message", sourceMessage);
try {
request.getRequestDispatcher("login_ok.jsp").forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// 发送聊天信息
public void sendMessages(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Random random = new Random();
String from = request.getParameter("from"); //发言人
String face = request.getParameter("face"); //表情
String to = request.getParameter("to"); //接收者
String color = request.getParameter("color"); //字体颜色
String content = request.getParameter("content"); //发言内容
String sendTime = new Date().toLocaleString(); //发言时间
ServletContext application = getServletContext();
String sourceMessage = application.getAttribute("message").toString();
try {
//发言时间
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String action = request.getParameter("action");
if ("loginRoom".equals(action)) { //登录时,写入系统公告
this.loginRoom(request, response);
} else if ("sendMessage".equals(action)) { //发送聊天信息
this.sendMessages(request, response);
} else if ("getMessages".equals(action)) { //从XML文件中读取聊天信息
this.getMessages(request, response);
}
}
// 将页面重定向到显示聊天信息的页面
public void getMessages(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
try {
request.getRequestDispatcher("content.jsp").forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
// 登录时,写入系统公告
public void loginRoom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession();
String username = request.getParameter("username"); //获得登录用户名
UserInfo user = UserInfo.getInstance(); //获得UserInfo类的对象
session.setMaxInactiveInterval(600); //设置Session的过期时间为10分钟
Vector vector = user.getList();
boolean flag = true; //标记是否登录的变量
//判断用户是否登录
System.out.println("vector的size:" + vector.size());
if (vector != null && vector.size() > 0) {
for (int i = 0; i < vector.size(); i++) {
System.out.println("vector" + i + ":" + vector.elementAt(i) + " user:" + username);
if (username.equals(vector.elementAt(i))) {
PrintWriter out;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
flag = false;
break;
}
// 发送聊天信息
public void sendMessages(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Random random = new Random();
String from = request.getParameter("from"); //发言人
String face = request.getParameter("face"); //表情
String to = request.getParameter("to"); //接收者
String color = request.getParameter("color"); //字体颜色
String content = request.getParameter("content"); //发言内容
String sendTime = new Date().toLocaleString(); //发言时间
ServletContext application = getServletContext();
String sourceMessage = application.getAttribute("message").toString();
try {
//发言时间
sourceMessage += "<font color='blue'><strong>" + from + "</strong></font><font color='#CC0000'>" + face + "</font>对<font color='green'>[" + to + "]</font>说:" + "<font color='" + color + "'>" + content + "</font>(" + sendTime + ")<br>";
application.setAttribute("message", sourceMessage);
request.getRequestDispatcher("Messages?action=getMessages&nocache=" + random.nextInt(10000)).forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
/**
* 用户注册的Servlet类
*/
public class RegServlet extends HttpServlet {
// 获取密码
String password = request.getParameter("password");
// 获取性别
String sex = request.getParameter("sex");
// 获取头像
String photo = request.getParameter("photo");
// 获取联系电话
String tel = request.getParameter("tel");
// 获取电子邮箱
String email = request.getParameter("email");
// 实例化UserDao对象
UserDao userDao = new UserDao();
if(username != null && !username.isEmpty()){
if(userDao.userIsExist(username)){
// 实例化一个User对象
User user = new User();
// 对用户对象中的属性赋值
user.setUsername(username);
user.setPassword(password);
user.setSex(sex);
user.setPhoto(photo);
user.setTel(tel);
user.setEmail(email);
// 保存用户注册信息
userDao.saveUser(user);
request.setAttribute("info", "恭喜,注册成功!<br>");
}else{
request.setAttribute("info", "错误:此用户名已存在!");
}
}
// 转发到message.jsp页面
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
/**
* 用户登录Servlet类
*/
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* 用户登录Servlet类
*/
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 实例化UserDao对象
UserDao userDao = new UserDao();
// 根据用户密码查询用户
User user = userDao.login(username, password);
// 判断user是否为空
if (user != null) {
// 将用户对象放入session中
request.getSession().setAttribute("user", user);
// 转发到result.jsp页面
request.getRequestDispatcher("Messages?action=loginRoom").forward(request, response);
} else {
// 登录失败
request.setAttribute("info", "错误:用户名或密码错误!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}
public class UserListener implements HttpSessionBindingListener {
private String user;
private UserInfo container = UserInfo.getInstance(); //获得UserInfo类的对象
public UserListener() {
user = "";
}
user.setPassword(password);
user.setSex(sex);
user.setPhoto(photo);
user.setTel(tel);
user.setEmail(email);
// 保存用户注册信息
userDao.saveUser(user);
request.setAttribute("info", "恭喜,注册成功!<br>");
}else{
request.setAttribute("info", "错误:此用户名已存在!");
}
}
// 转发到message.jsp页面
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
/**
* 用户登录Servlet类
*/
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 实例化UserDao对象
UserDao userDao = new UserDao();
// 根据用户密码查询用户
User user = userDao.login(username, password);
// 判断user是否为空
if (user != null) {
// 将用户对象放入session中
break;
}
}
}
//保存用户信息
if (flag) {
UserListener ul = new UserListener(); //创建UserListener的对象
ul.setUser(username); //添加用户
user.addUser(ul.getUser()); //添加用户到UserInfo类的对象中
session.setAttribute("user", ul); //将UserListener对象绑定到Session中
session.setAttribute("username", username); //保存当前登录的用户名
session.setAttribute("loginTime", new Date().toLocaleString()); //保存登录时间
ServletContext application = getServletContext();
String sourceMessage = "";
if (null != application.getAttribute("message")) {
sourceMessage = application.getAttribute("message").toString();
}
sourceMessage += "系统公告:<font color='gray'>" + username + "走进了聊天室!</font><br>";
application.setAttribute("message", sourceMessage);
try {
request.getRequestDispatcher("login_ok.jsp").forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// 发送聊天信息
public void sendMessages(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Random random = new Random();
String from = request.getParameter("from"); //发言人
String face = request.getParameter("face"); //表情
String to = request.getParameter("to"); //接收者
String color = request.getParameter("color"); //字体颜色
String content = request.getParameter("content"); //发言内容
String sendTime = new Date().toLocaleString(); //发言时间
ServletContext application = getServletContext();
String sourceMessage = application.getAttribute("message").toString();
try {
//发言时间
sourceMessage += "<font color='blue'><strong>" + from + "</strong></font><font color='#CC0000'>" + face + "</font>对<font color='green'>[" + to + "]</font>说:" + "<font color='" + color + "'>" + content + "</font>(" + sendTime + ")<br>";
application.setAttribute("message", sourceMessage);
request.getRequestDispatcher("Messages?action=getMessages&nocache=" + random.nextInt(10000)).forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
// 发送聊天信息
public void sendMessages(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Random random = new Random();
String from = request.getParameter("from"); //发言人
String face = request.getParameter("face"); //表情
String to = request.getParameter("to"); //接收者
String color = request.getParameter("color"); //字体颜色
String content = request.getParameter("content"); //发言内容
String sendTime = new Date().toLocaleString(); //发言时间
ServletContext application = getServletContext();
String sourceMessage = application.getAttribute("message").toString();
try {
//发言时间
sourceMessage += "<font color='blue'><strong>" + from + "</strong></font><font color='#CC0000'>" + face + "</font>对<font color='green'>[" + to + "]</font>说:" + "<font color='" + color + "'>" + content + "</font>(" + sendTime + ")<br>";
application.setAttribute("message", sourceMessage);
request.getRequestDispatcher("Messages?action=getMessages&nocache=" + random.nextInt(10000)).forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
/**
* 用户注册的Servlet类
*/
public class RegServlet extends HttpServlet {
public class Messages extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String action = request.getParameter("action");
if ("loginRoom".equals(action)) { //登录时,写入系统公告
this.loginRoom(request, response);
} else if ("sendMessage".equals(action)) { //发送聊天信息
this.sendMessages(request, response);
} else if ("getMessages".equals(action)) { //从XML文件中读取聊天信息
this.getMessages(request, response);
}
}
// 将页面重定向到显示聊天信息的页面
public void getMessages(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
try {
request.getRequestDispatcher("content.jsp").forward(request, response);
} catch (Exception ex) {
String face = request.getParameter("face"); //表情
String to = request.getParameter("to"); //接收者
String color = request.getParameter("color"); //字体颜色
String content = request.getParameter("content"); //发言内容
String sendTime = new Date().toLocaleString(); //发言时间
ServletContext application = getServletContext();
String sourceMessage = application.getAttribute("message").toString();
try {
//发言时间
sourceMessage += "<font color='blue'><strong>" + from + "</strong></font><font color='#CC0000'>" + face + "</font>对<font color='green'>[" + to + "]</font>说:" + "<font color='" + color + "'>" + content + "</font>(" + sendTime + ")<br>";
application.setAttribute("message", sourceMessage);
request.getRequestDispatcher("Messages?action=getMessages&nocache=" + random.nextInt(10000)).forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
/**
* 用户注册的Servlet类
*/
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 5280356329609002908L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 获取性别
String sex = request.getParameter("sex");
// 获取头像
String photo = request.getParameter("photo");
// 获取联系电话
String tel = request.getParameter("tel");
// 获取电子邮箱
String email = request.getParameter("email");
// 实例化UserDao对象
UserDao userDao = new UserDao();
try {
//发言时间
sourceMessage += "<font color='blue'><strong>" + from + "</strong></font><font color='#CC0000'>" + face + "</font>对<font color='green'>[" + to + "]</font>说:" + "<font color='" + color + "'>" + content + "</font>(" + sendTime + ")<br>";
application.setAttribute("message", sourceMessage);
request.getRequestDispatcher("Messages?action=getMessages&nocache=" + random.nextInt(10000)).forward(request, response);
} catch (Exception ex) {
Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
/**
* 用户注册的Servlet类
*/
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 5280356329609002908L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 获取性别
String sex = request.getParameter("sex");
// 获取头像
String photo = request.getParameter("photo");
// 获取联系电话
String tel = request.getParameter("tel");
// 获取电子邮箱
String email = request.getParameter("email");
// 实例化UserDao对象
UserDao userDao = new UserDao();
if(username != null && !username.isEmpty()){
if(userDao.userIsExist(username)){
// 实例化一个User对象
User user = new User();
// 对用户对象中的属性赋值
user.setUsername(username);
user.setPassword(password);
/**
* 用户登录Servlet类
*/
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 实例化UserDao对象
UserDao userDao = new UserDao();
// 根据用户密码查询用户
User user = userDao.login(username, password);
// 判断user是否为空
if (user != null) {
// 将用户对象放入session中
request.getSession().setAttribute("user", user);
// 转发到result.jsp页面
request.getRequestDispatcher("Messages?action=loginRoom").forward(request, response);
} else {
// 登录失败
request.setAttribute("info", "错误:用户名或密码错误!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}
public class UserListener implements HttpSessionBindingListener {
private String user;
private UserInfo container = UserInfo.getInstance(); //获得UserInfo类的对象
public UserListener() {
user = "";
}
// 设置在线监听人员
public void setUser(String user) {
this.user = user;
}
// 获取在线监听
public String getUser() {
return this.user;
}
@Override
public String getServletInfo() {
return "Short description";
}
}
/**
* 用户注册的Servlet类
*/
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 5280356329609002908L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 获取性别
String sex = request.getParameter("sex");
// 获取头像
String photo = request.getParameter("photo");
// 获取联系电话
String tel = request.getParameter("tel");
// 获取电子邮箱
String email = request.getParameter("email");
// 实例化UserDao对象
UserDao userDao = new UserDao();
if(username != null && !username.isEmpty()){
if(userDao.userIsExist(username)){
// 实例化一个User对象
User user = new User();
// 对用户对象中的属性赋值
user.setUsername(username);
user.setPassword(password);
user.setSex(sex);
// 转发到message.jsp页面
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
/**
* 用户登录Servlet类
*/
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 实例化UserDao对象
UserDao userDao = new UserDao();
// 根据用户密码查询用户
User user = userDao.login(username, password);
// 判断user是否为空
if (user != null) {
// 将用户对象放入session中
request.getSession().setAttribute("user", user);
// 转发到result.jsp页面
request.getRequestDispatcher("Messages?action=loginRoom").forward(request, response);
} else {
// 登录失败
request.setAttribute("info", "错误:用户名或密码错误!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}
/**
* 用户登录Servlet类
*/
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 实例化UserDao对象
UserDao userDao = new UserDao();
// 根据用户密码查询用户
User user = userDao.login(username, password);
// 判断user是否为空
if (user != null) {
// 将用户对象放入session中
request.getSession().setAttribute("user", user);
// 转发到result.jsp页面
request.getRequestDispatcher("Messages?action=loginRoom").forward(request, response);
} else {
// 登录失败
request.setAttribute("info", "错误:用户名或密码错误!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}