diff --git a/src/com/landaiqing/dao/UserDao.java b/src/com/landaiqing/dao/UserDao.java new file mode 100644 index 0000000..1fb96ef --- /dev/null +++ b/src/com/landaiqing/dao/UserDao.java @@ -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 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 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); + } + } +} diff --git a/src/com/landaiqing/entity/UserEntity.java b/src/com/landaiqing/entity/UserEntity.java new file mode 100644 index 0000000..491718f --- /dev/null +++ b/src/com/landaiqing/entity/UserEntity.java @@ -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; + } +} diff --git a/src/com/landaiqing/filter/UserSessionFilter.java b/src/com/landaiqing/filter/UserSessionFilter.java index 413ccd0..81b4145 100644 --- a/src/com/landaiqing/filter/UserSessionFilter.java +++ b/src/com/landaiqing/filter/UserSessionFilter.java @@ -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); + } +} diff --git a/src/com/landaiqing/service/UserService.java b/src/com/landaiqing/service/UserService.java new file mode 100644 index 0000000..d015888 --- /dev/null +++ b/src/com/landaiqing/service/UserService.java @@ -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 list() { + return userDao.list(); + } + /** + * 发布留言 + * */ + public int insert(UserEntity userEntity){ + return userDao.insert(userEntity); + } +} diff --git a/src/com/landaiqing/servlet/ListServlet.java b/src/com/landaiqing/servlet/ListServlet.java new file mode 100644 index 0000000..e4706ce --- /dev/null +++ b/src/com/landaiqing/servlet/ListServlet.java @@ -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 userEntities=userService.list(); + req.setAttribute("list",userEntities); + req.getRequestDispatcher("index.jsp").forward(req,resp); + } +} diff --git a/src/com/landaiqing/servlet/PublishServlet.java b/src/com/landaiqing/servlet/PublishServlet.java new file mode 100644 index 0000000..91d9445 --- /dev/null +++ b/src/com/landaiqing/servlet/PublishServlet.java @@ -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); + } + } +} diff --git a/web/index.jsp b/web/index.jsp index 1d30fef..285d7c1 100644 --- a/web/index.jsp +++ b/web/index.jsp @@ -9,12 +9,12 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> - + 留言本 <%-- Lay-ui组件库--%> -<%-- --%> -<%-- <%– Lay-ui样式–%>--%> -<%-- --%> + <%-- --%> + <%-- <%– Lay-ui样式–%>--%> + <%-- --%> @@ -22,429 +22,481 @@ + // - + - + -
+
-
-
- 留   言   本 -
- - -
-
    -
  • - - - -
    - -
    -
  • -
-
+
+
+ 留   言   本 +
+ + +
+
    +
  • + + + +
    + +
    +
  • +
+
-
+
- 留言列表 + 留言列表