添加发布评论功能

This commit is contained in:
2023-06-09 15:18:06 +08:00
parent 23b265e957
commit 345c2614cf
9 changed files with 788 additions and 424 deletions

View File

@@ -0,0 +1,75 @@
package com.landaiqing.dao;
import com.landaiqing.entity.UserEntity;
import com.landaiqing.utils.JdbcUtils;
import java.sql.*;
import java.util.ArrayList;
public class UserDao {
/**
* 留言列表查询
* */
private UserEntity userEntity=new UserEntity();
public ArrayList<UserEntity> list() {
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
String listSql = "select * from user;";
preparedStatement = connection.prepareStatement(listSql);
resultSet = preparedStatement.executeQuery();
ArrayList<UserEntity> userEntities=new ArrayList<>();
while (resultSet.next()) {
// 将db中数据 返回给客户端 查询到数据
Integer userId = resultSet.getInt(1);
String nickName = resultSet.getString(2);
String qq = resultSet.getString(3);
String email = resultSet.getString(4);
String content = resultSet.getString(5);
Date dateTime = resultSet.getDate(6);
String replyContent = resultSet.getString(7);
Date replyDateTime = resultSet.getDate(8);
UserEntity userEntity = new UserEntity(userId, nickName, qq, email, content, dateTime, replyContent, replyDateTime);
userEntities.add(userEntity);
}
return userEntities;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
}
}
/**
* 发表评论
* */
public int insert(UserEntity userEntity){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
JdbcUtils.beginTransaction(connection);
preparedStatement = connection.prepareStatement("INSERT INTO `webguestbook`.`user` (`userId`, `nickname`, `qq`, `email`, `content`, `datetime`, `replyContent`, `replyDateTime`) VALUES (null, ?, ?, ?, ?, ?,null, null);\n");
// preparedStatement.setInt(1,userEntity.getUserId());
preparedStatement.setString(1,userEntity.getNickName());
preparedStatement.setString(2,userEntity.getQQ());
preparedStatement.setString(3,userEntity.getEmail());
preparedStatement.setString(4,userEntity.getContent());
preparedStatement.setDate(5, new Date(userEntity.getDateTime().getTime()));
Integer result = preparedStatement.executeUpdate();
JdbcUtils.commitTransaction(connection);
return result;
} catch (SQLException e) {
JdbcUtils.rollBackTransaction(connection);
throw new RuntimeException(e);
} finally {
JdbcUtils.closeConnection(null, preparedStatement, connection);
}
}
}

View File

@@ -0,0 +1,110 @@
package com.landaiqing.entity;
import java.sql.Date;
public class UserEntity {
/**
* CREATE TABLE `user` (
* `userId` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
* `nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
* `qq` int DEFAULT NULL COMMENT 'QQ',
* `email` varchar(255) DEFAULT NULL COMMENT '邮箱',
* `content` longtext COMMENT '留言内容',
* `datetime` datetime DEFAULT NULL COMMENT '留言日期',
* `replyContent` longtext COMMENT '回复内容',
* `replyDateTime` datetime DEFAULT NULL COMMENT '回复时间',
* PRIMARY KEY (`userId`) USING BTREE
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
* */
private Integer userId;
private String nickName;
private String QQ;
private String email;
private String content;
private Date dateTime;
private String replyContent;
private Date replyDateTime;
public UserEntity(){
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getQQ() {
return QQ;
}
public void setQQ(String QQ) {
this.QQ = QQ;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
}
public String getReplyContent() {
return replyContent;
}
public void setReplyContent(String replyContent) {
this.replyContent = replyContent;
}
public Date getReplyDateTime() {
return replyDateTime;
}
public void setReplyDateTime(Date replyDateTime) {
this.replyDateTime = replyDateTime;
}
public UserEntity(Integer userId, String nickName, String QQ, String email, String content, Date dateTime, String replyContent, Date replyDateTime) {
this.userId = userId;
this.nickName = nickName;
this.QQ = QQ;
this.email = email;
this.content = content;
this.dateTime = dateTime;
this.replyContent = replyContent;
this.replyDateTime = replyDateTime;
}
}

View File

@@ -1,48 +1,48 @@
//package com.landaiqing.filter;
//
//
//import jakarta.servlet.*;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import jakarta.servlet.http.HttpSession;
//
//import java.io.IOException;
//
///**
// * 过滤器
// */
//@WebFilter("/*")// 过滤器所有的请求
//public class UserSessionFilter implements Filter {
// private String[] excludeUrls = new String[]{"/login", "/register", "/VerifycodeServlet"};
//
// @Override
// public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// // 从session获取到用户的会话信息 判断用户是否登录过
// HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
// HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
// String contextPath = httpServletRequest.getContextPath();
// // 定义一个数组 哪些 请求是需要排除的
// for (int i = 0; i < excludeUrls.length; i++) {
// String excludeUrl = contextPath + excludeUrls[i];
// String requestURI = httpServletRequest.getRequestURI();
// if (excludeUrl.equals(requestURI)) {
// // 放行请求
// filterChain.doFilter(httpServletRequest, httpServletResponse);
// return;
// }
// }
// // 排除请求
// HttpSession session = httpServletRequest.getSession();
// Object user = session.getAttribute("user");
// if (user == null) {
// // 当前用户没有登录或者登录会话失效
// // 重定向到登录页面
// httpServletResponse.sendRedirect(contextPath+"/login");
// return;
// }
// // 用户已经登录了 正常放行请求
// filterChain.doFilter(httpServletRequest, httpServletResponse);
// }
//}
package com.landaiqing.filter;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
/**
* 过滤器
*/
@WebFilter("/System/*")// 过滤器所有的请求
public class UserSessionFilter implements Filter {
private String[] excludeUrls = new String[]{"/login", "/register", "/VerifycodeServlet"};
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// 从session获取到用户的会话信息 判断用户是否登录过
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
String contextPath = httpServletRequest.getContextPath();
// 定义一个数组 哪些 请求是需要排除的
for (int i = 0; i < excludeUrls.length; i++) {
String excludeUrl = contextPath + excludeUrls[i];
String requestURI = httpServletRequest.getRequestURI();
if (excludeUrl.equals(requestURI)) {
// 放行请求
filterChain.doFilter(httpServletRequest, httpServletResponse);
return;
}
}
// 排除请求
HttpSession session = httpServletRequest.getSession();
Object user = session.getAttribute("user");
if (user == null) {
// 当前用户没有登录或者登录会话失效
// 重定向到登录页面
httpServletResponse.sendRedirect(contextPath+"/login");
return;
}
// 用户已经登录了 正常放行请求
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}

View File

@@ -0,0 +1,22 @@
package com.landaiqing.service;
import com.landaiqing.dao.UserDao;
import com.landaiqing.entity.UserEntity;
import java.util.ArrayList;
public class UserService {
private UserDao userDao=new UserDao();
/**
* 留言列表查询
*/
public ArrayList<UserEntity> list() {
return userDao.list();
}
/**
* 发布留言
* */
public int insert(UserEntity userEntity){
return userDao.insert(userEntity);
}
}

View File

@@ -0,0 +1,31 @@
package com.landaiqing.servlet;
import com.alibaba.fastjson.JSONObject;
import com.landaiqing.entity.UserEntity;
import com.landaiqing.service.UserService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
@WebServlet("")
public class ListServlet extends HttpServlet {
private UserService userService=new UserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<UserEntity> userEntities=userService.list();
req.setAttribute("list",userEntities);
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
}

View File

@@ -0,0 +1,70 @@
package com.landaiqing.servlet;
import com.landaiqing.entity.UserEntity;
import com.landaiqing.service.UserService;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
@WebServlet("/publish")
public class PublishServlet extends HttpServlet {
private UserService userService=new UserService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String name = req.getParameter("name");
if (StringUtils.isNullOrEmpty(name)){
req.setAttribute("errorMsg","name 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
String qq = req.getParameter("qq");
if (StringUtils.isNullOrEmpty(qq)){
req.setAttribute("errorMsg","qq 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
String email = req.getParameter("email");
if (StringUtils.isNullOrEmpty(email)){
req.setAttribute("errorMsg","arriveAirport 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
String content = req.getParameter("content");
if (StringUtils.isNullOrEmpty(content)){
req.setAttribute("errorMsg","content 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
Date time = new java.sql.Date(new java.util.Date().getTime());
UserEntity userEntity=new UserEntity();
userEntity.setNickName(name);
userEntity.setQQ(qq);
userEntity.setEmail(email);
userEntity.setContent(content);
userEntity.setDateTime((java.sql.Date) time);
int result = userService.insert(userEntity);
if (result<=0){
req.setAttribute("errorMsg","插入失败!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
resp.sendRedirect("index.jsp");
} catch (Exception e) {
req.setAttribute("errorMsg","系统异常!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
throw new RuntimeException(e);
}
}
}