三、部分代码
BookAction
package Action; import Dao.BookDao; import Entity.Book; import Entity.User; 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 javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class BookAction extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String action = request.getParameter("action"); if (action.equals("getall")) { this.getAll(request, response); } else if (action.equals("addtemp")) { this.addtemp(request, response); } else if (action.equals("gettemp")) { this.gettemp(request, response); } else if (action.equals("confirm")) { this.confirm(request, response); } else if (action.equals("querybookbyid")) { this.QueryBookById(request, response); } else if (action.equals("DeleteById")) { this.DeleteById(request, response); } else if (action.equals("EditDone")) { this.EditDone(request, response); } } private void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BookDao bdao = new BookDao(); ArrayList<Book> bookArrayList = bdao.getAllBook(); HttpSession session = request.getSession(); session.setAttribute("allbooklist", bookArrayList); request.getRequestDispatcher("/book-list.jsp").forward(request, response); } private void addtemp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String next = request.getParameter("over"); BookDao bdao = new BookDao(); Book book = new Book(); if (!request.getParameter("bookid").equals("")) book.setId(request.getParameter("bookid")); if (!request.getParameter("bookname").equals("")) book.setName(request.getParameter("bookname")); if (!request.getParameter("bookauthor").equals("")) book.setAuthor(request.getParameter("bookauthor")); if (!request.getParameter("bookpublisher").equals("")) book.setPublisher(request.getParameter("bookpublisher")); if (!request.getParameter("bookcategory").equals("")) book.setCategory(request.getParameter("bookcategory")); if (!request.getParameter("bookprice").trim().equals("")) book.setPrice(Integer.parseInt(request.getParameter("bookprice"))); if (!request.getParameter("bookstore").trim().equals("")) book.setStore(Integer.parseInt(request.getParameter("bookstore"))); if (!request.getParameter("booklocation").equals("")) book.setLocation(request.getParameter("booklocation")); if (!request.getParameter("bookdesc").equals("")) book.setDesc(request.getParameter("bookdesc")); if (!request.getParameter("bookid").equals("")) bdao.addtemp(book); if (next.equals("0")) request.getRequestDispatcher("/add-book.jsp").forward(request, response); else this.gettemp(request, response); } private void gettemp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BookDao bdao = new BookDao(); ArrayList<Book> addbooklist = bdao.getadd(); HttpSession session = request.getSession(); session.setAttribute("addbooklist", addbooklist); request.getRequestDispatcher("/add-confirm.jsp").forward(request, response); } private void confirm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BookDao bdao = new BookDao(); bdao.confirm(); this.getAll(request, response); } private void QueryBookById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BookDao bdao = new BookDao(); String id = request.getParameter("id"); String next = request.getParameter("next"); Book book = bdao.QueryBookById(id); HttpSession session = request.getSession(); session.setAttribute("resultbook", book); PrintWriter out = response.getWriter(); if (next.equals("check")) request.getRequestDispatcher("/detail.jsp").forward(request, response); else if (next.equals("edit")) request.getRequestDispatcher("/edit-book.jsp").forward(request, response); else if (next.equals("borrowcheck")) { out.write(book.getName() + "||" + book.getAuthor() + "||" + book.getPublisher() + "||" + book.getRemain()); } } private void DeleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BookDao bdao = new BookDao(); String id = request.getParameter("id"); bdao.DeleteById(id); this.getAll(request, response); } private void EditDone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BookDao bdao = new BookDao(); Book book = new Book(); if (!request.getParameter("id").equals("")) book.setId(request.getParameter("id")); if (!request.getParameter("name").equals("")) book.setName(request.getParameter("name")); if (!request.getParameter("author").equals("")) book.setAuthor(request.getParameter("author")); if (!request.getParameter("publisher").equals("")) book.setPublisher(request.getParameter("publisher")); if (!request.getParameter("category").equals("")) book.setCategory(request.getParameter("category")); if (!request.getParameter("price").trim().equals("")) book.setPrice(Integer.parseInt(request.getParameter("price"))); if (!request.getParameter("store").trim().equals("")) book.setStore(Integer.parseInt(request.getParameter("store"))); if (!request.getParameter("location").equals("")) book.setLocation(request.getParameter("location")); if (!request.getParameter("desc").equals("")) book.setDesc(request.getParameter("desc")); if (!request.getParameter("id").equals("")) bdao.addtemp(book); bdao.EditDone(book); request.getRequestDispatcher("BookAction?action=querybookbyid&id=<%=b.getId()%>&next=check").forward(request, response); } }
IOAction
package Action; import Dao.IODao; import Entity.Log; 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 javax.servlet.http.HttpSession; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; public class IOAction extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if (action.equals("borrow")) { this.borrow(request, response); } else if (action.equals("getlog")) { this.getlog(request, response); } else if (action.equals("return")) { this.ReturnBook(request, response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void borrow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { IODao ioDao = new IODao(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date date = new Date(); String time = sdf.format(date); String readerid = request.getParameter("readerid"); String bookid = request.getParameter("bookid"); int borrowday = Integer.parseInt(request.getParameter("borrowday")); ioDao.borrow(bookid, readerid, time, borrowday); this.getlog(request, response); } protected void getlog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { IODao ioDao = new IODao(); ArrayList<Log> loglist = ioDao.getLogList(); HttpSession session = request.getSession(); session.setAttribute("loglist", loglist); request.getRequestDispatcher("/io-log.jsp").forward(request, response); } protected void ReturnBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { IODao ioDao = new IODao(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date date = new Date(); String Returntime = sdf.format(date); String bookid = request.getParameter("bookid"); String readerId = request.getParameter("ReaderId"); String borrowtime = request.getParameter("borrowtime"); ioDao.ReturnBook(bookid, readerId, borrowtime, Returntime); this.getlog(request, response); } }
LoginAction
package Action; import Dao.UserDao; import Entity.User; 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 javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; public class LoginAction extends HttpServlet { UserDao udao = new UserDao(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if (action.equals("login")) { this.login(request, response); } else if (action.equals("logout")) { this.logout(request, response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = null; String password = null; PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); username = request.getParameter("username"); password = request.getParameter("password"); User user = new User(); user.setUsername(username); user.setPassword(password); String result = udao.login(user); if (result.equals("true")) { session.setAttribute("adminname", user.getName()); request.getRequestDispatcher("/main.jsp").forward(request, response); } else { out.write(result); } } private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setMaxInactiveInterval(1); response.sendRedirect("/index.jsp"); } }
ReaderAction
package Action; /** * 读者管理 */ import Dao.IODao; import Dao.ReaderDao; import Entity.Log; import Entity.Reader; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class ReaderAction extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=utf-8"); if (action.equals("QueryReaderById")) { this.QueryReaderById(request, response); } else if (action.equals("GetBorrowListById")) { this.GetBorrowListById(request, response); } else if (action.equals("GetAllReader")) { this.GetAllReader(request, response); } else if (action.equals("SetBlackList")) { this.SetBlackList(request, response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void QueryReaderById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String readerid = request.getParameter("readerid"); ReaderDao readerDao = new ReaderDao(); Reader reader = readerDao.QueryReaderById(readerid); String stauts = (reader.getStatus() == 1) ? "正常" : "异常"; String callback = reader.getName() + "||" + reader.getGrade() + "年级" + reader.getClassnum() + "班||" + stauts + "||" + reader.getBorrow(); PrintWriter out = response.getWriter(); out.write(callback); } protected void GetBorrowListById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("readerid"); IODao ioDao = new IODao(); ArrayList<Log> loglist = ioDao.QueryBorrowNumByReaderid(id); HttpSession session = request.getSession(); session.setAttribute("loglist", loglist); request.getRequestDispatcher("/borrow-list.jsp").forward(request, response); } protected void GetAllReader(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ReaderDao readerDao = new ReaderDao(); ArrayList<Reader> readerlist = readerDao.GetAllReader(); HttpSession session = request.getSession(); session.setAttribute("readerlist", readerlist); request.getRequestDispatcher("/reader-list.jsp").forward(request, response); } protected void SetBlackList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("id"); String edit = request.getParameter("edit"); ReaderDao readerDao = new ReaderDao(); if (edit.equals("设置为正常")) { readerDao.SetBlackList(username, true); } else { readerDao.SetBlackList(username, false); } this.GetAllReader(request, response); } }
四、其他
1.其他系统实现
1.JavaWeb系统系列实现
Java+JSP实现学生图书管理系统
Java+JSP实现学生信息管理系统
Java+JSP实现用户信息管理系统
Java+Servlet+JSP实现航空订票系统
Java+Servlet+JSP实现新闻发布系统
Java+Servlet+JSP实现停车场管理系统
Java+Servlet+JSP实现学生信息管理系统
Java+Servlet+JSP实现学生选课管理系统
Java+Servlet+JSP实现学生成绩管理系统-1
Java+Servlet+JSP实现学生成绩管理系统-2
Java+Servlet+JSP实现宠物诊所管理系统
Java+SSM+JSP实现网上考试系统
Java+SSH+JSP实现在线考试系统
Java+SSH+JSP实现医院在线挂号系统
Java+Springboot+Mybatis+Bootstrap+Maven实现网上商城系统
2.JavaSwing系统系列实现
Java+Swing实现斗地主游戏
Java+Swing实现图书管理系统
Java+Swing实现医院管理系统
Java+Swing实现考试管理系统
Java+Swing实现仓库管理系统-1
Java+Swing实现仓库管理系统-2
Java+Swing实现自助取款机系统
Java+Swing实现通讯录管理系统
Java+Swing实现停车场管理系统
Java+Swing实现学生信息管理系统
Java+Swing实现学生宿舍管理系统
Java+Swing实现学生选课管理系统
Java+Swing实现学生成绩管理系统
Java+Swing实现学校教材管理系统
Java+Swing实现学校教务管理系统
Java+Swing实现企业人事管理系统
Java+Swing实现电子相册管理系统
Java+Swing实现超市管理系统-TXT存储数据
Java+Swing实现自助取款机系统-TXT存储数据
Java+Swing实现宠物商店管理系统-TXT存储数据