# java web期末项目-简单网络聊天室
**Repository Path**: sjjshs/chatroom
## Basic Information
- **Project Name**: java web期末项目-简单网络聊天室
- **Description**: java web期末项目,使用servlet , jsp , vue , elementplus 写的简单网络聊天室
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 6
- **Forks**: 2
- **Created**: 2025-01-16
- **Last Updated**: 2026-01-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: Java, Servlet, Tomcat, Vue, Element-UI
## README
# 项目配置
```
这是我的开发配置
jdk17
tomcat10
Mysql8
数据库配置位置:com.fdhwws.web.servlet.InitServlet
lib库位置:web\WEB-INF\lib
核心用户admin(管理员)------用户名:admin 密码:123456
注意:后台数据库'密码'字段都是加密过的,请使用com.fdhwws.web.util.InfoSecurityUtil加解密
关于dao层的数据库操作用的是我之前写的工具类---MyJdbcUtil
https://gitee.com/sjjshs/jdbc-utility-class MyJdbcUtil的仓库位置
```
# 项目介绍
这是我的期末项目,开发只有7天,时间比较赶,所以凑活用吧 (对于我的预期来说是半成品,因为比较赶,做了一些取舍)
至于为什么都用vue了还要和jsp结合 ............ 因为老师要求
时间问题,通过轮询实现聊天更新,不是web socket!!!
写的一般,能用就行
# 项目浏览
## 前台
### 聊天展示(需要两个不同的浏览器)

### 登录/注册


### 创建房间


### 加入房间



### 我的房间


### 聊天室足迹

## 后台管理员(只要登录用户为管理员即可)
### 用户管理

### 房间管理


# 二次开发指南
## 关于servlet开发
``` java
//以如下类为例,必须继承BaseServlet
@WebServlet("/api/chatRoomMsg/*") //根api,注意最后要带*号
public class ChartRoomMsgAPIServlet extends BaseServlet {
private final ChatRoomMsgService chatRoomMsgService = new ChatRoomMsgServiceImpl();
private final UserFootprintsService userFootprintsService = new UserFootprintsServiceImpl();
/**
* 函数名字就是指定的api
* 如下api函数的完整url是 [根路径[ip:端口等等]/api/chatRoomMsg/getChatRoomMsg]
* 关于返回值 1.返回值为字符串则直接返回到前端,不进行处理 2.如果是其他任意对象(除了字符串),会处理成json数据然后返回
* 关于函数参数,必须携带HttpUtil参数,这个参数可以获取request response对象等
* 所以后续添加api就通过这个方法实现,只需要添加函数就行
**/
public AjaxResult getChatRoomMsg(HttpUtil httpUtil) throws Exception {
// 这个函数用于身份验证
AjaxResult ajaxResult = verifyUser(httpUtil);
if (!ajaxResult.isSuccess()) {
return ajaxResult;
}
// User user = (User) ajaxResult.data; 获取当前登录用户
// Number roomId = httpUtil.getNumberParameter("roomId"); 获取parameter 等等操作
try {
// 如果前端传输的是json数据,就可以通过这个函数直接获取对象,会自动反序列化
ChatRoom jsonBody = httpUtil.getJSONBody(ChatRoom.class);
AjaxResult chatRoomMsg = chatRoomMsgService.getChatRoomMsg(jsonBody.getId(), jsonBody.getPassword());
if (ajaxResult.isSuccess()) {
List data = (List) chatRoomMsg.data;
data.forEach(ChatRoomMsg::safeMode);
}
return chatRoomMsg;
} catch (Exception e) {
e.printStackTrace();
}
return AjaxResult.error("获取聊天室消息失败", null);
}
}
// 关于跳转页面
// api访问路径 [根路径[ip:端口等等]/api/chatRoomMsg/test]
public String test(HttpUtil httpUtil) {
// return httpUtil.redirect("/xxxx/xxxx"); //重定向
return httpUtil.forward("/xxxx/xxxx") //请求转发
}
```
## 关于dao层开发
``` java
//实体类
//必须实现IMyEntity接口
// @TableName("my_new_table") 类名对应的表名 my_new_table, 如果不添加默认类名为表名, 注意一定要和数据库里的表名相对应
public class ChatRoom implements IMyEntity { //对应mysql表名 chat_room
//注意变量名字对应数据表的列名,如果不使用MyJdbcUtil里的实体类操作,可以不写ColumnField
//表示只注入,一般用于自增字段,不会保存到数据库,只会获取数据
@ColumnField(type = ColumnField.Type.SET, isPrimaryKey = true)
private Integer id; //对应id列
//普通字段
//@ColumnField(colName = "my_name") 可以指定列名
@ColumnField
private String name; //对应name列
// @JSONField //表示进行json化处理的字段可选.会自动json处理
@ColumnField
private Integer access;
@ColumnField
private String password;
@ColumnField
private Boolean valid;
@ColumnField
private String pic;
@ColumnField
private Integer createUserId;
@ColumnField
private Timestamp createTime;
public ChatRoom() {
}
public ChatRoom(Integer id, String name, Integer access, String password, Boolean valid, String pic, Integer createUserId, Timestamp createTime) {
this.id = id;
this.name = name;
this.access = access;
this.password = password;
this.valid = valid;
this.pic = pic;
this.createUserId = createUserId;
this.createTime = createTime;
}
// 必须要get,set函数
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAccess() {
return access;
}
public void setAccess(Integer access) {
this.access = access;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Boolean getValid() {
return valid;
}
public void setValid(Boolean valid) {
this.valid = valid;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public Integer getCreateUserId() {
return createUserId;
}
public void setCreateUserId(Integer createUserId) {
this.createUserId = createUserId;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
public ChatRoom safeMode(boolean showPassword) {
if (showPassword && !StringUtils.isNullOrEmpty(this.password)) {
this.password = InfoSecurityUtil.decrypt(this.password);
} else {
this.password = null;
}
createUserId = null;
createTime = null;
return this;
}
public void passwordEncrypt() {
if (this.password != null) {
this.password = InfoSecurityUtil.encrypt(this.password);
}
}
@Override
public String toString() {
return "ChartRoom{" +
"id=" + id +
", name='" + name + '\'' +
", access=" + access +
", password='" + password + '\'' +
", valid=" + valid +
", pic='" + pic + '\'' +
", createUserId=" + createUserId +
", createTime=" + createTime +
'}';
}
}
// 不用改 这些是最基本的函数接口
public interface BaseDao {
public T selectById(Integer id);
public List selectAll();
public int insert(T entity);
public int update(T entity);
public int delete(Integer id);
}
//这些是特殊功能的函数接口
//需要继承BaseDao,并泛型指定实体类
public interface ChatRoomDao extends BaseDao {
public ChatRoom selectChartRoomByName(String name);
public List selectChatRoomsByUid(Integer uid);
}
//dao层实现类,实现上述接口
public class ChatRoomDaoImpl implements ChatRoomDao {
@Override
public ChatRoom selectById(Integer id) {
//这是我封装的jdbc控制类,详细讲起来比较复杂,详情见仓库[https://gitee.com/sjjshs/jdbc-utility-class?source=header_my_projects]
//原始方法
//Connection connection = MyJdbcUtil.getInstance().getConnection(); //直接获取connection,注意不要close!!!!!
/* 基本用法(对实体类没有规范要求):
//查询数据
// 方式1 查询数据获取行列数组
ArrayList