第一次Java web的完整小项目,题目来自2020春季学期Java web开发技术课程设计。 团队成员3人 我负责的是管理端的图书修、删、查找,用户端的图书借阅、图书查找,以及部分可视化页面设计。 非常学生风格的成品,没什么技术含量,就简单纪念一下,因为是合作项目,所以只给出部分内容的思路。 一、CSS+HTML的可视化页面。 因为开发工具的问题,几乎没有涉及JavaScript的使用,所以会使代码看起来繁琐且不专业。 1、登陆后的用户端主页面: 默认显示是新书推荐页面,点击书名也可进行借书。 welcome_user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Library</title> <link href="user.css" type="text/css" rel="stylesheet"> </head> <body> <% String user = (String) request.getAttribute("account"); session.setAttribute("account", user); %> <div id="header"> <h1 style="margin-bottom: 0;"><%=user%>同学,Welcome to library </h1> </div> <div class="bar"> <ul> <li><a href="ServletSkip?userdo=newbook&user=<%=user%>" target="menuFrame"> 新书推荐 </a></li> <li><a href="return_reservation.jsp" target="menuFrame"> 归还图书 </a></li> <li><a href="ServletSkip?userdo=query&user=<%=user%>&do=null" target="menuFrame"> 借阅图书 </a></li> <li><a href="#"> 图书分类 </a> <ul> <li><a href="ServletSkip?userdo=lit&user=<%=user%>">文学</a></li> <li><a href="ServletSkip?userdo=nature&user=<%=user%>">科学</a></li> <li><a href="ServletSkip?userdo=soc&user=<%=user%>">社会</a></li> <li><a href="ServletSkip?userdo=tec&user=<%=user%>">技术</a></li> </ul></li> </ul> </div> <div class="new"> <iframe id="menuFrame" name="menuFrame" src="NewBook.jsp" style="overflow: visible;" scrolling="auto" frameborder="no" width="100%" height="100%"; float:left"> </iframe> </div> <div id="footer">All books are authentic, and the final interpretation right belongs to the library</div> </body> </html>user.css
@charset "UTF-8"; body{ margin:0; padding:0; } #header{ background-color:#d3abcb; width:100%; height:50px; text-align:center; top: 0; } .new{ background-color:#EEEEEE; height:700px; width:100%; float:left; } #footer{ height:10%; background-color:#866d92; clear:both; text-align:center; } .bar{ text-align:center; height:40px; width:100%; background:rgb(189, 181, 181); margin-left: 0; float:right; } .bar ul{ list-style-type: none; white-space:nowrap; overflow: hidden; margin-top: 0; margin-left:29%; padding: 0; } .bar li { float:left; margin-right:2%; position: relative; overflow: hidden; text-align:center; display:inline; } .bar li a{ display: block; color:white; padding: 3px; overflow: hidden; text-decoration: none; text-align:center; } .bar li a:hover{ background-color: #111; } .bar ul li ul{ text-align:center; margin-left: -0.2px; background:rgb(193, 176, 187); position: relative; display: none; } .bar ul li ul li{ float:none; } .bar ul li:hover ul{ display: block; }NewBook.jsp(涉及到的ServletOperate.java后面有给出)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> .new{ background-color:#EEEEEE; height:100%; width:100%; float:left; } h1 a{ color:black; padding: 3px; overflow: hidden; text-decoration: none; } h2{ color:black; padding: 3px; } img{ height:200px; width:160px; border-radius: 8px; }</style> <title>Insert title here</title> </head> <body> <div class="new" > <h1><a href="ServletOperate?indexs=T8561&sort=bno">云边有个小卖部</a></h1> <h2>【中】张嘉佳</h2> <img src="timg.jpg"/> <p> 张嘉佳2018新作,连续三周获得畅销书榜首 云边镇少年刘十三的成长故事。 <h1><a href="ServletOperate?indexs=B7829&sort=bno">没有思想的世界</a></h1> <h2>【美】富兰克林·福尔(Franklin Foer)</h2> <img src="aaa.jpg"/> <p>纽约时报、洛杉矶时报、美国国家公共广播电台年度影响力著作。 <h1><a href="ServletOperate?indexs=S8249&sort=bno">西川美和:围绕电影的X</a></h1> <h2>【日】西川美和</h2> <img src="bbb.jpg"/> <p>囊括日本电影蓝丝带最佳导演奖、横滨电影节最佳导演奖、《电影旬报》年度十佳第一名。 倍受是枝裕和赞誉、当代日本女性导演的翘楚--西川美和,首部中文随笔集。 <h1><a href="ServletOperate?indexs=S4632&sort=bno">朴赞郁的蒙太奇</a></h1> <h2>【韩】朴赞郁</h2> <img src="ccc.jpg"/> <p>本书收录了朴赞郁1997年到2005年间的文章及电影札记,记录了《共同警备区》《我要复仇》《老男孩》《亲切的金子》等重要电影的诞生过程及创作理念,导演对世界的看法,对习以为常现象的思考,对经典电影的剖析都闪烁着独特的智慧。 </div> </body> </html>2、管理端操作主页面 welocme_manager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> * { margin: 0px; padding: 0px; } .header{ text-align: center; width:100%; height:75px; background-color:#d3abcb; } .main{ width:100%; height:1000px; } .daohang{ width:15%; height:100%; float:left; background-color:rgb(189, 181, 181); } ul{ white-space: nowrap; overflow: hidden; text-align: center; } li{ overflow: hidden; margin-bottom:10px; } li a{ display: block; color: white; text-align: center; padding: 3px; overflow: hidden; text-decoration: none; } li a:hover{ background-color: #111; } .content{ height:100%; background-color:#866d92; } </style> </head> <body> <div class="header"> <h2>Welcome,manager</h2> <h3>you can do the following</h3> </div> <div class="main"> <div class="daohang"><ul> <li><a href="#" target="menuFrame">增加图书</a></li> <li><a href="#" target="menuFrame">删除用户</a></li> <li><a href="#" target="menuFrame">查询用户</a></li> <li><a href="#" target="menuFrame">更改图书</a></li> </ul></div> <div class="content"><iframe id="menuFrame" name="menuFrame" src="announce_manager.jsp" style=" background-color: #EEEEEE; width:84%; height:1000px; float:right ;frameborder:0"> </iframe></div> </div> </body> </html>二、管理端对图书的修、删、查找。 1、修删(弊端是:数据库中所有的图书全部列出,优化的话,可以专门分出想要修删的类别) 可视化页面: change_book.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="object.*"%> <%@ page import="dao.*"%> <%@ page import="java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>管理员_图书编辑</title> <style type="text/css"> a:link,a:visited{ text-decoration: none; color:#0000CD; display:block; text-align:center; background-color:#D4F2E7; } a:active,a:hover{ background-color:#00CED1; } </style> </head> <body> <h2 align="center">Operate</h2> <table border="0" bgcolor="#ccceee" align="center"> <tr bgcolor="#CCCCCC" align="center"> <th>书号</th> <th>书名</th> <th>作者</th> <th>价格</th> <th>简介</th> <th>操作</th> <th></th> </tr> <% BookDao bd = new BookDao(); ArrayList<Books> list = bd.findall(); if (list == null || list.size() < 1) { out.print("没有数据!"); } else { for (Books book : list) { %> <tr align="center" > <td><%=book.getBno()%></td> <td><%=book.getBname()%></td> <td><%=book.getBauthor()%></td> <td><%=book.getBprice()%></td> <td><%=book.getBremark()%></td> <td><a href="ServletBook?do=editbefore&bno=<%=book.getBno()%>">更改</a></td> <td><a href="ServletBook?do=del&bno=<%=book.getBno()%>">删除</a></td> </tr> <% } } %> </table> </body> </html>edit_book.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="beans.*"%> <%@ page import="dao.*"%> <%@ page import="java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>编辑书籍</title> </head> <body> <% //String a =(String)session.getAttribute("editbook"); //BookDao bd = new BookDao(); //Books book=bd.find(a); Book a =(Book)session.getAttribute("editbook"); String b=""+a.getBprice(); %> <form action="ServletBookEdit"> <table> <tr> <td>书号:</td> <td><input name="bnoo" value="<%=a.getBno()%>"></td> </tr> <tr> <td>书名:</td> <td><input name="bname" value="<%=a.getBname() %>"></td> </tr> <tr> <td>作者:</td> <td><input name="bauthor" value="<%=a.getBauthor() %>"></td> </tr> <tr> <td>价格:</td> <td><input name="bprice" value="<%=b%>"></td> </tr> <tr> <td>简介:</td> <td><textarea name="bremark" cols="50" rows="7" ><%=a.getBremark() %></textarea><</td> </tr> </table> <input type="submit" value="修改"></td> <input type="reset" value="取消"></td> </form> </body> </html>
需要的bean:Books.java
public class Books { private String Bno;//书号 private String Bname;//书名 private String Bauthor;//作者名 private String Bremark;//简介 private double Bprice;//价格 private int Bbrrow;//借阅状态 private int Breserve;//预约状态 public Books() { super(); // TODO 自动生成的构造函数存根 } public Books(String bno, String bname, String bauthor, double bprice, String bremark) { super(); Bno = bno; Bname = bname; Bauthor = bauthor; Bremark = bremark; Bprice = bprice; } public String getBno() { return Bno; } public void setBno(String bno) { Bno = bno; } public String getBname() { return Bname; } public void setBname(String bname) { Bname = bname; } public String getBauthor() { return Bauthor; } public void setBauthor(String bauthor) { Bauthor = bauthor; } public String getBremark() { return Bremark; } public void setBremark(String bremark) { Bremark = bremark; } public double getBprice() { return Bprice; } public void setBprice(double bprice) { Bprice = bprice; } public int getBbrrow() { return Bbrrow; } public void setBbrrow(int bbrrow) { Bbrrow = bbrrow; } public int getBreserve() { return Breserve; } public void setBreserve(int breserve) { Breserve = breserve; } }DataConn.java(连接数据库,以下使用不再赘述)
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DataConn { private static String drivername="com.mysql.cj.jdbc.Driver"; private static String username="root"; private static String password="root"; private static String dbs="lms"; public static Connection getConnection() throws Exception { String url1="jdbc:mysql://localhost:3306/"+dbs+"?useSSL=false&serverTimezone=UTC"; try { Class.forName(drivername); Connection conn=DriverManager.getConnection(url1,username,password); return conn; } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } public static void closeConnection(Connection conn,PreparedStatement ptsm,ResultSet rs) { try { if(rs!=null) rs.close(); if(ptsm!=null) ptsm.close(); if(conn!=null) conn.close(); } catch(SQLException e) { e.printStackTrace(); } } }需要的Dao:BookDao.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.mysql.cj.xdevapi.Result; import bean.DataConn; import object.Books; public class BookDao implements IBookDao { private static final String PreparedStatement = null; protected static String sql = "Bno, Bname,Bauthor,Bprice, Bremark,Bbrrow,Breserve"; protected static String delete_sql = "delete from books where Bno=?"; protected static String change_sql = "update books set Bno=?,Bname=?,Bauthor=?,Bprice=?,Bremark=? where Bno=?"; public BookDao() { super(); // TODO 自动生成的构造函数存根 } public Books create(Books book) throws Exception { return null; } public Books find(String bno) throws Exception { Connection conn = null; Books bs = new Books(); conn = DataConn.getConnection(); Statement ptsm = conn.createStatement(); String select = "select * from books where Bno=" + "'" + bno + "'"; ResultSet rs = ptsm.executeQuery(select); if (rs.next()) { bs.setBno(rs.getString(1)); bs.setBname(rs.getString(2)); bs.setBauthor(rs.getString(3)); bs.setBprice(rs.getDouble(4)); bs.setBremark(rs.getString(5)); bs.setBbrrow(rs.getInt(6)); bs.setBreserve(rs.getInt(7)); } ptsm.close(); rs.close(); conn.close(); return bs; } public void delete(String bno) throws Exception { Connection conn = null; PreparedStatement ptsm = null; ResultSet rs = null; conn = DataConn.getConnection(); ptsm = conn.prepareStatement(delete_sql); ptsm.setString(1, bno); ptsm.executeUpdate(); DataConn.closeConnection(conn, ptsm, rs); } public void change(Books book) throws Exception { Connection conn = null; PreparedStatement ptsm = null; ResultSet rs = null; conn = DataConn.getConnection(); ptsm = conn.prepareStatement(change_sql); ptsm.setString(1, book.getBno()); ptsm.setString(2, book.getBname()); ptsm.setString(3, book.getBauthor()); ptsm.setDouble(4, book.getBprice()); ptsm.setString(5, book.getBremark()); ptsm.setString(6, book.getBno()); int a = ptsm.executeUpdate(); DataConn.closeConnection(conn, ptsm, rs); } public ArrayList<Books> findall() throws Exception { ArrayList<Books> booklist = new ArrayList<Books>(); String sql = "select * from books"; Connection conn = null; PreparedStatement ptsm = null; ResultSet rs = null; conn = DataConn.getConnection(); ptsm = conn.prepareStatement(sql); rs = ptsm.executeQuery(); try { while (rs.next()) { Books u = new Books(rs.getString(1), rs.getString(2), rs.getString(3), rs.getDouble(4), rs.getString(5)); booklist.add(u); } } catch (SQLException e) { e.printStackTrace(); } DataConn.closeConnection(conn, ptsm, rs); return booklist; } public ArrayList<Books> findfuzzy(String sort, String indexs) throws Exception { ArrayList<Books> booklist = new ArrayList<Books>(); Connection conn = null; Books bs = new Books(); conn = DataConn.getConnection(); String select = "select * from books where " + sort + " like'%" + indexs + "%'"; PreparedStatement pstmt = conn.prepareStatement(select); ResultSet rs = pstmt.executeQuery(); try { while (rs.next()) { Books u = new Books(rs.getString(1), rs.getString(2), rs.getString(3), rs.getDouble(4), rs.getString(5)); u.setBbrrow(rs.getInt(6)); u.setBbrrow(rs.getInt(7)); booklist.add(u); } } catch (SQLException e) { e.printStackTrace(); } pstmt.close(); rs.close(); conn.close(); return booklist; } }需要的Servlet: ServletBook.java
import java.io.IOException; import java.io.PrintWriter; import java.util.List; 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 dao.BookDao; import object.Books; /** * Servlet implementation class ServletBook */ public class ServletBook extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); BookDao bd = new BookDao(); String dos = request.getParameter("do"); if (dos == null || dos.equals("")) { dos = "index"; } if (dos.equals("index")) { response.sendRedirect("change_book.jsp"); } if (dos.equals("del")) { String bno = request.getParameter("bno"); try { bd.delete(bno); out.print("<script>alert('删除成功!');window.location='ServletBook?do=index';</script>"); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (dos.equals("editbefore")) { try { String kkk = request.getParameter("bno"); Books bs=new Books(); bs = bd.find(kkk); session.setAttribute("editbook", bs); response.sendRedirect("edit_book.jsp"); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }ServleBooktEdit.java
import java.io.IOException; import java.io.PrintWriter; 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 dao.BookDao; import object.Books; /** * Servlet implementation class ServletBookEdit */ public class ServletBookEdit extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); BookDao bd = new BookDao(); String bno = request.getParameter("bnoo"); String bname = request.getParameter("bname"); String ba = request.getParameter("bauthor"); String bp = request.getParameter("bprice"); Double b2 = Double.parseDouble(bp); String br = request.getParameter("bremark"); Books bs = new Books(bno, bname, ba, b2, br); bs.setBbrrow(1); bs.setBreserve(1); try { bd.change(bs); out.print("<script>alert('修改成功!');window.location='ServletBook?do=index';</script>"); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }2、查找 模糊查找很粗暴,作者、书名或书号。 query_book.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" 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> <center> <form action="ServletSelectBook"> <input type="text" name="indexs" size="30"><br> <input name="sort" value="bno" type="radio" checked />书号 <input name="sort" value="bname" type="radio">书名 <input name="sort" value="bauthor" type="radio">作者<br> <input type="submit" value="查询"> <input type="reset" value="取消"> </form> </center> </body> </html>query_result.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="beans.*"%> <%@ page import="dao.*"%> <%@ page import="java.util.*"%> <!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> <% String a = (String) session.getAttribute("s"); String b = (String) session.getAttribute("result"); BookDao bd = new BookDao(); ArrayList<Book> Book = bd.findfuzzy(a, b); if (Book == null || Book.size() < 1) { out.print("没有数据!"); } else { for (Book book : Book) { %> <h1> 《<%=book.getBname()%>》 </h1> <h2> 书号:<%=book.getBno()%></h2> <h2> 作者:<%=book.getBauthor()%></h2> <h2> 价格:<%=book.getBprice()%></h2> <p> 简介: <%=book.getBremark()%></p> <% } } %> </body> </html>需要的bean:Books.java BookDao.java 需要的Dao:DataConn.java 需要的servlet:ServletOperate.java
import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; 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 dao.BookDao; import object.Books; /** * Servlet implementation class ServletOperate */ public class ServletOperate extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); HttpSession session = request.getSession(); String sort = request.getParameter("sort"); String indexs=request.getParameter("indexs"); String t=(String)getServletContext().getAttribute("usersno"); if (sort == null || sort.equals("")) { response.sendRedirect("query_book.jsp"); } else { if(sort.equals("bno")) try { String s="Bno"; session.setAttribute("s", s); } catch (Exception e) { e.printStackTrace(); } if (sort.equals("bname")) { try { String s="Bname"; session.setAttribute("s", s); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (sort.equals("bauthor")) { try { String s="Bauthor"; session.setAttribute("s", s); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } session.setAttribute("result", indexs); session.setAttribute("xh", t); response.sendRedirect("query_result.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }三、用户端的查找与借书 在此之前,需要把登录端成功提交的用户名传值到ServletSkip.java中 登录端的可视化页面就不放出来了,servlet如下
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class ServletSkip */ public class ServletSkip extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); String userdos=request.getParameter("userdo"); String sno=request.getParameter("user"); if(userdos.equals("query")) { //session.setAttribute("usersno", sno); session.getServletContext().setAttribute("usersno",sno); response.sendRedirect("query_book.jsp"); } if(userdos.equals("newbook")) { //session.setAttribute("usersno", sno); session.getServletContext().setAttribute("usersno",sno); response.sendRedirect("NewBook.jsp"); } if(userdos.equals("nature")) { //session.setAttribute("usersno", sno); session.getServletContext().setAttribute("usersno",sno); response.sendRedirect("nature.jsp"); } if(userdos.equals("lit")) { //session.setAttribute("usersno", sno); session.getServletContext().setAttribute("usersno",sno); response.sendRedirect("literature.jsp"); } if(userdos.equals("soc")) { //session.setAttribute("usersno", sno); session.getServletContext().setAttribute("usersno",sno); response.sendRedirect("society.jsp"); } if(userdos.equals("tec")) { //session.setAttribute("usersno", sno); session.getServletContext().setAttribute("usersno",sno); response.sendRedirect("technology.jsp"); } if(userdos.equals("")||userdos==null) { out.println("网页错误"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }1、查找 比管理端的模糊查找多了分类查找,可以根据书号的编写方法,分别查找文学、科学、社会、技术类别的书籍 每一大类还有许多小类,可根据图书简介下的借书状态,判断是否要借 例如:文学(literature.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> * { margin: 0px; padding: 0px; } .main{ width: 100%; height: 1000px; background-color:red; float: left; } .daohang{ width: 10%; float:left; height: 100%; background-color:rgb(189, 181, 181); } ul{ white-space: nowrap; overflow: hidden; text-align: center; } li{ overflow: hidden; margin-bottom:10px; } li a{ display: block; color: white; text-align: center; padding: 3px; overflow: hidden; text-decoration: none; } li a:hover{ background-color: #111; } .yemian{ height: 100%; background-color:#EEEEEE; } </style> </head> <body> <div class="main"> <div class="daohang"> <ul> <li><a href="ServletOperate?indexs=S&sort=bno" target="menuFrame">散文</a></li> <li><a href="ServletOperate?indexs=X&sort=bno" target="menuFrame">古典</a></li> <li><a href="ServletOperate?indexs=Z&sort=bno" target="menuFrame">通俗</a></li> <li><a href="ServletOperate?indexs=T&sort=bno" target="menuFrame">纪实</a></li> </ul> </div> <div class="yemian"> <iframe id="menuFrame" name="menuFrame" src="" style=" background-color: #adb9cd; width:89.5%; height:1000px; float:right ;frameborder:0"> </iframe> </div> </div> </body> </html>需要的bean、servlet如上所示 2、借书 借书时会直接跳转至模糊查找的界面: 这里的query_book.jsp、servlet、bean、dao与管理端的相同 这里更改一下的query_result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="beans.*"%> <%@ page import="dao.*"%> <%@ page import="java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> body { background-color: #EEEEEE; text-align: center; } a { display: block; color: black; text-align: center; padding: 5px; margin: 10px; overflow: hidden; text-decoration: none; background-color: white } a:active, a:hover { background-color: #C5E3E3 } </style> </head> <body> <% String d = (String) session.getAttribute("xh"); %> <h1><%=d%></h1> <% String a = (String) session.getAttribute("s"); String b = (String) session.getAttribute("result"); BookDao bd = new BookDao(); ArrayList<Book> Book = bd.findfuzzy(a, b); if (Book == null || Book.size() < 1) { out.print("没有数据!"); } else { for (Book book : Book) { %> <h1> 《<%=book.getBname()%>》 </h1> <h2> 书号:<%=book.getBno()%></h2> <h2> 作者:<%=book.getBauthor()%></h2> <h2> 价格:<%=book.getBprice()%></h2> <p> 简介: <%=book.getBremark()%></p> <% if (book.getBborrow() == 0) { %> <a href="ServletBorrow?sno=<%=d%>&bno=<%=book.getBno()%>">借书</a> <% } else { %> <a href="#">已出借</a> <% } } } %> </body> </html>增加的ServletBorrow.java:
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; 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 dao.BookDao; import dao.SBBDao; import dao.StudentDao; import dao.TimeDao; import object.Books; import object.SBB; import object.Student; /** * Servlet implementation class ServletBorrow */ public class ServletBorrow extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); HttpSession session = request.getSession(); String sno=request.getParameter("sno"); String bno=request.getParameter("bno"); StudentDao sd=new StudentDao(); SBBDao sbbd=new SBBDao(); BookDao bd=new BookDao(); Student stu=new Student(); Date d = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(d); int year=calendar.get(Calendar.YEAR); int month=calendar.get(Calendar.MONTH); int day=calendar.get(Calendar.DATE); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); int second=calendar.get(Calendar.SECOND); TimeDao td=new TimeDao(year,month,day,hour,minute,second); String sbb1=td.SBB1(); String sbb2=td.SBB2(); SBB sb=new SBB(sno,bno,sbb1,"0",sbb2,0); try { stu=sd.find(sno); Books bs=bd.find(bno); int m=stu.getSborrow(); if(m<8) { bs.setBbrrow(0); bs.setBreserve(0); bd.change(bs); sbbd.Insert(sb); stu.setSborrow(m+1); sd.change(stu); session.setAttribute("student", stu); response.sendRedirect("borrow_success.jsp"); } else { response.sendRedirect("borrow_fail.jsp"); } } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }这里还需给出dao:TimeDao.java(用来计算应还的日期)
public class TimeDao { private int year; private int month; private int day; private int hour; private int minute; private int second; public TimeDao() { super(); // TODO 自动生成的构造函数存根 } public TimeDao(int year, int month, int day, int hour, int minute, int second) { super(); this.year = year; this.month = month; this.day = day; this.hour = hour; this.minute = minute; this.second = second; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } public int getMinute() { return minute; } public void setMinute(int minute) { this.minute = minute; } public int getSecond() { return second; } public void setSecond(int second) { this.second = second; } public int Runnian() { if(year%100!=0&&year%4==0) { return 1; } else { return 0;} } public String SBB1() { String sbb1=year+"-"+month+"-"+day; return sbb1; } public String SBB2() { int year2; int month2; int day2; int[] time={31,28,31,30,31,30,31,31,30,31,30,31}; if(Runnian()==1) { time[1]=29; }else { time[1]=28; } if(month==12) { month2=1; year2=year+1; day2=day; } else { if(time[month]<31) { if(day==31) { month2=month+2; day2=day-time[month]; } else { month2=month+1; day2=day; } } else { month2=month+1; day2=day; } year2=year; } return year2+"-"+month2+"-"+day2; } }以及Bean:StudentDao.java(查找、更改学生的借书的本数)
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import beans.JdbcUtil; import beans.Student; public class StudentDao { protected static String change_sql = "update Students set Sborrow=? where Sno=?"; public Student find(String sno) throws Exception { Connection conn = null; Student bs = new Student(); conn = JdbcUtil.getConnection(); Statement ptsm = conn.createStatement(); String select="select * from students where Sno='"+sno+"'"; ResultSet rs = ptsm.executeQuery(select); if (rs.next()) { bs.setSno(rs.getString(1)); bs.setSname(rs.getString(2)); bs.setSclass(rs.getInt(3)); bs.setSgrade(rs.getInt(4)); bs.setSprofession(rs.getString(5)); bs.setSborrow(rs.getInt(6)); } ptsm.close(); rs.close(); conn.close(); return bs; } public void change(Student stu) throws Exception { Connection conn = null; PreparedStatement ptsm = null; ResultSet rs = null; conn =JdbcUtil.getConnection(); ptsm = conn.prepareStatement(change_sql); ptsm.setInt(1, stu.getSborrow()); ptsm.setString(2, stu.getSno()); int n=ptsm.executeUpdate(); JdbcUtil.free(rs, ptsm, conn); } }SBBDao.java(增添学生借书的记录)
import java.sql.Connection; import java.sql.PreparedStatement; import beans.JdbcUtil; import beans.SBB; public class SBBDao { public void Insert(SBB b) throws Exception { Connection conn=null; conn =JdbcUtil.getConnection(); String sql="insert into SBB(Sno,Bno,Brodate,Retdate,SRetdate,Reborrow) values(?,?,?,?,?,?);"; PreparedStatement ptsm= conn.prepareStatement(sql); ptsm.setString(1, b.getSno()); ptsm.setString(2,b.getBno()); ptsm.setString(3, b.getBrodate()); ptsm.setString(4, b.getRetdate()); ptsm.setString(5,b.getSRetdate()); ptsm.setInt(6, b.getReborrow()); int n=ptsm.executeUpdate(); JdbcUtil.free(null, ptsm, conn); } }若学生已借书的本数小于8本,则跳转至成功页面(borrow_success.jsp),否则跳转至失败页面(borrow_fail.jsp) borrow_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="beans.*"%> <%@ page import="dao.*"%> <%@ page import="java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Student s = (Student) session.getAttribute("student"); %> <h1 align="center"><%=s.getSname()%>借书成功!</h1> </body> </html>borrow_fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1 align="center">您已达导借书数量最大限度!</h1> <h1 align="center">抱歉借书失败!!!</h1> </body> </html>