一、系统介绍
软件环境
Operating System:Windows10
IDEA:2018.2
Java:jdk1.8
Mysql:8.0.13
Tomcat:7.0.85
该图书管理系统实现了用户注册与登录功能,实现了实现了图书列表展示,购物车简单的功能。后台表只有三张,一张是user表,存储的是用户的信息,一张是book表,存储的是图书的信息,另外一张是card表,用来存储某个用户对应的购物车的信息。
下面是整个整个工程的截图
二、系统展示
1.登录界面
2.注册界面
3.图书列表界面
4.图书详情界面
5.购物车界面
三、部分代码
1.前端
book.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/24 Time: 10:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ page import="java.util.List" %> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.service.impl.BookServiceImpl" %> <%@ page import="com.sjsq.po.User" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>查看图书</title> <style type="text/css"> h1{ text-align: center; } #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <div id="head"> <table width="100%"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="cart.jsp">我的购物车</a> <a href="logout.jsp">安全退出</a> </td> </table> </div> <% } %> <%--图书信息--%> <% Book book = new Book(); BookServiceImpl service = new BookServiceImpl(); List<Book> list = service.select(book); %> <h1>图书列表</h1> <div id="before"> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> <table align="center" cellpadding="10" cellspacing="10"> <tr bgcolor="green"> <td>编号</td> <td>书名</td> <td>价格</td> <td>作者</td> <td>封皮</td> <td>出版社</td> </tr> <% String bg = null; for (int i = 0;i<list.size();i++){ Book b =list.get(i); if(i%2 == 0){ bg = "pink"; }else{ bg = "yellow"; } %> <tr bgcolor="<%=bg%>"> <td><%=b.getBookid()%></td> <td><a href="doInfo.jsp?bookid=<%=b.getBookid()%>"><%=b.getBookname()%></a></td> <td><%=b.getPrice() %></td> <td><%=b.getAuthor() %></td> <td><%=b.getPicture() %></td> <td><%=b.getPublish() %></td> </tr> <% } %> </table> </body> </html>
cart.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/31 Time: 10:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="com.sjsq.po.User" %> <%@ page import="com.sjsq.po.Card" %> <%@ page import="com.sjsq.service.CardService" %> <%@ page import="com.sjsq.service.impl.CardServiceImpl" %> <%@ page import="java.util.List" %> <%@ page import="com.sjsq.dao.BookDao" %> <%@ page import="com.sjsq.dao.impl.BookDaoImpl" %> <html> <head> <title>购物车信息</title> <style type="text/css"> h1{ text-align: center; } #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <div id="head"> <table width="100%"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="cart.jsp">我的购物车</a> <a href="logout.jsp">安全退出</a> </td> </table> </div> <% } %> <%--图书信息--%> <% CardService service = new CardServiceImpl(); List<Card> list = service.getCard(user.getId()); BookDao dao = new BookDaoImpl(); Double totalPrice = 0D; Double bookPrice = 0D; %> <h1>购物车图书</h1> <div id="before"> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> <table align="center" cellpadding="10" cellspacing="10"> <tr bgcolor="green"> <td>姓名</td> <td>图书序号</td> <td>书名</td> <td>数量</td> <td>价格小计</td> </tr> <% for (int i = 0;i<list.size();i++){ Card card = list.get(i); String bookName = dao.getBook(card.getBookid()).getBookname(); bookPrice = dao.getBook(card.getBookid()).getPrice()*card.getBooknum(); totalPrice = bookPrice + totalPrice; %> <tr bgcolor="#ffdead"> <td><%=card.getUsername() %></td> <td><%=card.getBookid() %></td> <td><%=bookName %></td> <td><%=card.getBooknum() %></td> <td><%=bookPrice%></td> </tr> <% } %> <tr> <td colspan="4" align="right" bgcolor="#6495ed">价格总计</td> <td bgcolor="#6495ed"><%=totalPrice %></td> </tr> </table> <div style="text-align:center;font-size:20px;margin-top:20px;"> <a href="book.jsp">继续购买图书</a> <a href="login.jsp">登陆页面</a> </div> </body> </html>
detail.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/24 Time: 10:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.po.User" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>图书详情</title> <style type="text/css"> h1{ text-align: center; } a{ text-align:center;font-size: 24px;text-decoration: none; } a:hover{ text-decoration: underline;font-size: 20px; } #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <div id="head"> <table width="100%"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="cart.jsp">我的购物车</a> <a href="logout.jsp">安全退出</a> </td> </table> </div> <% } %> <h1>图书详情</h1> <div id="before"> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> <% Book book = (Book)session.getAttribute("book"); %> <table align="center" cellpadding="20" cellspacing="20"> <tr style="font-size: 20px"> <td>图书编号</td> <td>图书名称</td> <td>图书价格</td> <td>图书作者</td> <td>图书封皮</td> <td>图书出版社</td> </tr> <tr> <td><%=book.getBookid()%></td> <td><%=book.getBookname()%></td> <td><%=book.getPrice()%></td> <td><%=book.getAuthor()%></td> <td><%=book.getPicture()%></td> <td><%=book.getPublish()%></td> </tr> <tr> <td colspan="3"></td> <td></td> <td colspan="2"></td> </tr> </table> <div style="text-align:center;font-size: 36px;"> <a href="doCart.jsp">添加到购物车</a> <a href="book.jsp">图书列表</a> </div> </body> </html>
doCart.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/31 Time: 10:40 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.po.User" %> <%@ page import="com.sjsq.service.CardService" %> <%@ page import="com.sjsq.service.impl.CardServiceImpl" %> <html> <head> <title>处理购物车</title> </head> <body> <%-- 处理购物车 --%> <% // 获取用户的信息 User user =(User)session.getAttribute("user"); // 获取书籍的信息 Book book = (Book)session.getAttribute("book"); // 定义购物车服务 CardService service = new CardServiceImpl(); // 获取图书数量且加1 Integer booknum = service.getBookNum(book) + 1; // 执行添加购物车操作 boolean flag = service.addCard(user,book,booknum); if(flag){ // 添加成功返回到购物车页面 response.sendRedirect("cart.jsp"); }else{ // response.sendRedirect("doCartFail.jsp"); } %> </body> </html>
doCartFail.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/31 Time: 11:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加失败</title> <style type="text/css"> h1{ text-align: center; } h4{ text-align: center;color: red; } body{ background-color: antiquewhite; } div{ text-align: center; } </style> </head> <body> <h1>添加购物车失败</h1> <hr> <h4>请重新添加</h4> <div> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> </body> </html>
doInfo.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/24 Time: 10:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.service.BookService" %> <%@ page import="com.sjsq.service.impl.BookServiceImpl" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>书籍信息</title> </head> <body> <% Book book = new Book(); String sid = request.getParameter("bookid"); Integer id = Integer.parseInt(sid); BookService service = new BookServiceImpl(); book.setBookid(id); Book bookCur = service.getBook(book); // 控制台打印出类的信息(日志的前身) System.out.print("doInfo.jsp的信息-->"); System.out.println(bookCur); session.setAttribute("book", bookCur); response.sendRedirect("detail.jsp"); %> </body> </html>
doregister.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/21 Time: 23:45 To change this template use File | Settings | File Templates. --%> <%@page import="com.sjsq.dao.impl.UserDaoImpl"%> <%@page import="com.sjsq.dao.UserDao"%> <%@page import="com.sjsq.po.User"%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>处理注册</title> </head> <body> <% // 设置获取注册时的编码为UTF-8 request.setCharacterEncoding("UTF-8"); User user=new User(); //获取register.jsp页面提交的账号和密码 String name=request.getParameter("name"); String password=request.getParameter("password"); String email=request.getParameter("email"); String phone=request.getParameter("phone"); //获取register.jsp页面提交的账号和密码设置到实体类User中 user.setName(name); user.setPassword(password); user.setEmail(email); user.setPhone(phone); //引入数据交互层 UserDao dao=new UserDaoImpl(); boolean flag=dao.register(user); if(flag){ response.sendRedirect("login.jsp"); }else{ response.sendRedirect("register.jsp"); } %> </body> </html>
dosearchPassword.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/22 Time: 23:23 To change this template use File | Settings | File Templates. --%> <%@page import="java.util.List"%> <%@page import="com.sjsq.service.impl.UserServiceImpl"%> <%@page import="com.sjsq.po.User"%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>处理找回密码</title> </head> <body> <% User user=new User(); //获取searchPassword.jsp页面提交的账号和密码 String name=request.getParameter("name"); user.setName(name); UserServiceImpl service=new UserServiceImpl(); List<User> list=service.selectUser(user); request.setAttribute("list", list); for(User u:list){ request.setAttribute("user", u); out.print(u); } if(user!=null){ //response.sendRedirect("search.jsp");//不传输数据的转发 request.getRequestDispatcher("search.jsp").forward(request, response); } %> </body> </html>
fail.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/26 Time: 23:14 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录失败</title> <style type="text/css"> h1{ text-align: center; } h4{ text-align: center;color: red; } body{ background-color: antiquewhite; } div{ text-align: center; } </style> </head> <body> <h1>现存图书列表</h1> <hr> <h4>登录失败</h4> <div> <a href="login.jsp">返回登录</a> </div> </body> </html>
info.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/26 Time: 22:56 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ page import="com.sjsq.po.User"%> <%@ page import="com.sjsq.dao.UserDao"%> <%@ page import="com.sjsq.dao.impl.UserDaoImpl"%> <%@ page import="com.sjsq.service.UserService" %> <%@ page import="com.sjsq.service.impl.UserServiceImpl" %> <%@ page import="java.util.List" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户登录</title> <style type="text/css"> h1{ text-align: center; } h4{ text-align: center;color: red; } body{ background-color: antiquewhite; } </style> </head> <body> <h1>现存图书列表</h1> <hr> <h4>---装饰中---</h4> <% // 设置接收的编码为UTF-8 request.setCharacterEncoding("utf-8"); User user = new User(); UserDao dao = new UserDaoImpl(); String name = request.getParameter("name"); String password=request.getParameter("password"); user.setName(name); user.setPassword(password); User us=dao.login(user); // 把数据库里面的User获取出来 UserService service = new UserServiceImpl(); List<User> list = service.selectUser(user); for(int i=0;i<list.size();i++){ user = list.get(i); } System.out.println("显示用户信息:"); System.out.println(user); session.setAttribute("user",user); if(us != null){ response.sendRedirect("book.jsp"); }else{ response.sendRedirect("fail.jsp"); } %> </body> </html>
login.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/19 Time: 22:44 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户登录</title> <style type="text/css"> h1{ text-align: center; } h4{ text-align: center;color: red; } body{ background-color: antiquewhite; } a{ text-decoration: none;font-size: 20px;color: black; } a:hover{ text-decoration: underline;font-size: 24px;color: red; } </style> </head> <body> <form action="info.jsp" method="post"> <h1>用户登录</h1> <h4>---正在美化中---</h4> <hr/> <table align="center"> <tr> <td>账号:</td> <td><input type="text" name="name" id="name" placeholder="请输入您的账号" autofocus="autofocus"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" id="password" placeholder="请输入您的密码"></td> <td><a href="searchPassword.jsp">找回密码</a></td> </tr> <tr> <td colspan="1"> </td> <td> <input type="submit" value="登录"/> <input type="reset" value="重置"/> <a href="register.jsp" target="_blank">注册</a> </td> </tr> </table> </form> </body> </html>
logout.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/25 Time: 21:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>退出登录</title> </head> <body> <% session.invalidate(); response.sendRedirect("login.jsp"); %> </body> </html>
register.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/21 Time: 23:14 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户注册</title> <style type="text/css"> h1{ text-align: center; } h4{ text-align: center;color: red; } body{ background-color: antiquewhite; } div{ text-align: center; } </style> </head> <body> <h1>用户注册</h1> <h4>---装饰中---</h4> <hr/> <form action="doregister.jsp" method="post" name="registerForm"> <div> <tr> <label>您的账号:</label> <input type="text" name="name" id="name" placeholder="请输入用户名" autofocus="autofocus"> </tr> </div> <div> <tr> <label>您的密码:</label></td> <input type="password" name="password" id="password" placeholder="请输入密码"> </tr> </div> <div> <tr> </tr> <label>确认密码:</label> <input type="password" name="relpassword" id="relpassword" placeholder="请确认密码"> </div> <div> <tr> <label>电话号码:</label> <input type="text" name="phone" id="phone" placeholder="请输入电话号码"> </tr> </div> <div> <tr> <label>电子邮件:</label> <input type="text" name="email" id="email" placeholder="请输入电子邮件"> </tr> </div> <div> <tr> <button type="submit" onclick="return checkForm()">注册</button> <button type="reset">重置</button> <a href="login.jsp" target="_blank">登录</a> </tr> </div> </form> <script type="text/javascript"> function checkForm() { var name = registerForm.name.value; var pwd = registerForm.password.value; var repwd = registerForm.relpassword.value; //alert(name + pwd + repwd); if (name == "" || name == null) { alert("请输入用户名"); registerForm.name.focus(); return false; } else if (pwd == "" || pwd == null) { alert("请输入密码"); registerForm.password.focus(); return false; } else if (repwd == "" || repwd == null) { alert("请输入确认密码"); registerForm.relpassword.focus(); return false; } else if (pwd != repwd) { alert("两次密码输入不一致,请重新输入!"); registerForm.relpassword.focus(); return false; } alert('注册成功!'); return true; } </script> </body> </html>
search.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/22 Time: 23:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>弹出信息</title> <script type="text/javascript"> alert("您的密码是:${user.password}"); </script> <style type="text/css"> h1{ text-align: center; } div{ text-align: center; } </style> </head> <body> <h1>您的密码是:${user.password}</h1> <div> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> </body> </html>
searchPassword.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/22 Time: 23:14 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>找回密码</title> <style type="text/css"> h1{ text-align: center; } div{ text-align: center; } body{ background-color:antiquewhite; } </style> </head> <body> <h1>找回密码</h1> <div> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> <form action="dosearchPassword.jsp" method="post"> <table align="center"> <tr> <td>请输入账号:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td colspan="1"></td> <td> <input type="submit" value="提交"> <input type="reset" value="重置"> </td> </tr> </table> </form> </body> </html>