diff --git a/src/com/hellogithub/dao/fileDao.java b/src/com/hellogithub/dao/fileDao.java index a218a02..2ce1f1e 100644 --- a/src/com/hellogithub/dao/fileDao.java +++ b/src/com/hellogithub/dao/fileDao.java @@ -40,4 +40,19 @@ public class fileDao { } return fileEntity; } + + public int addLookCount(int id ) + { + int num ; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql="update onefile set lookNum = lookNum + 1 where id = ?"; + num = runner.update(conn,sql,id); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return num; + } } diff --git a/src/com/hellogithub/service/oneFileService.java b/src/com/hellogithub/service/oneFileService.java index c744c24..fe829fe 100644 --- a/src/com/hellogithub/service/oneFileService.java +++ b/src/com/hellogithub/service/oneFileService.java @@ -41,4 +41,5 @@ public class oneFileService { fileEntity.setUserName(userName); return fileEntity; } + public int addLookCount(int id ){return fileDao.addLookCount(id);} } diff --git a/src/com/hellogithub/servlet/systemAdmin/addFileLookCountServlet.java b/src/com/hellogithub/servlet/systemAdmin/addFileLookCountServlet.java new file mode 100644 index 0000000..1b58b67 --- /dev/null +++ b/src/com/hellogithub/servlet/systemAdmin/addFileLookCountServlet.java @@ -0,0 +1,37 @@ +package com.hellogithub.servlet.systemAdmin; + +import com.hellogithub.service.oneFileService; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +@WebServlet("/addFileLookCount") +public class addFileLookCountServlet extends HttpServlet { + private oneFileService oneFileService = new oneFileService(); + @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 { + resp.setCharacterEncoding("UTF-8"); + resp.setContentType("text/html; charset=utf-8"); + // 设置响应头允许ajax跨域访问 + String curOrigin = req.getHeader("Origin"); + resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin); + resp.setHeader("Access-Control-Allow-Credentials", "true"); + resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD"); + resp.setHeader("Access-Control-Max-Age", "3600"); + resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); + resp.setContentType("application/json;charset=UTF-8"); + String id =req.getParameter("id"); + int num = oneFileService.addLookCount(Integer.parseInt(id)); + PrintWriter writer = resp.getWriter(); + writer.println(num); + } +}