Servlet+JSP+JDBC设计实现图书系统——管理功能实现

Servlet+JSP+JDBC设计实现图书系统——管理功能实现

2023年7月29日发(作者:)

Servlet+JSP+JDBC设计实现图书系统——管理功能实现写在前⾯,之前由于种种原因博客好久没有更新。最近打算重拾JavaWeb,所以从头开始,先⽤servlet+jdbc+bootstrap最基础的代码实现⼀个图书系统。考虑有管理员端+⽤户端,项⽬完成后会上传⾄github,后期会升级ssh/ssm等,毕竟是温故学习,⼀点⼀点来,项⽬会⼀直更新补充!  2018.04.16更新,管理端进⼀步完善,页⾯优化调整  2018.03.29更新,管理端实现了过滤功能(管理端⼤部分功能已经完善,只剩下JS/JQ的还没有,等⽤户端写完了再说)  2018.03.26更新,管理端添加图书编辑页⾯(),并采⽤了jquery的ajax技术  2018.03.22更新,管理端采⽤EL&JSTL进⾏了页⾯重构,并且添加分页功能1.项⽬结构│ .classpath│ .gitignore│ .project│ LICENSE│ │

├─.settings│ .jsdtscope│ │ │ ent│ │ │ │ ner│ │

├─build│ └─classes│ ├─biz│ │ │ │ │ │ │ │ │

│ │ └─impl│ │ │ │ │ │

│ ├─dao│ │ │ │ │ │ │ │ │

│ │ └─impl│ │ │ │ │ │

│ ├─pojo│ │ │ │ │ │ │ │

│ ├─servlet│ │ │ │ │ │ │ │ │ │

│ └─utils│ │

├─src│ ├─biz│ │ │ │ │ │ │ │ │

│ │ └─impl│ │ │ │ │ │

│ ├─dao│ │ │ │ │ │ │ │ │

│ │ └─impl│ │ │ │ │ │

│ ├─pojo│ │ │ │ │ │ │ │

│ ├─servlet│ │ │ │ │ │ │ │ │ │

│ └─utils│ │

└─WebContent├─css│ │ │ │ │ │ │ │ │

├─fonts│ │ │ │ │ 2│

├─img│ │ │ │ │ │ │ │ │ │ │ │ │ │

│ └─readme│ │

├─js│ │ │ │ │

├─META-INF│ │

├─web│ │ │ │

└─WEB-INF│ │

└─

  项⽬采取最基础的MVC分层架构,全部采⽤servlet+jdbc⽅式实现。jsp作为v层,servlet作为controller层与业务层代码关联,整个代码做到最⼤限度的低耦合。前端主要是jQuery和Bootstrap,毕竟我前端了解的少,仅仅会⽤⼏个API,所以整个项⽬前端不做过多赘述。2.项⽬分层概述  ①dao层主要是处理与数据库交互的逻辑  1 public interface AdminDao {2 public boolean adminLogin(String admin,String password);3 }   1 public class AdminDaoImpl implements AdminDao{ 2 public boolean adminLogin(String admin, String password) { 3 Connection conn=null; 4 PreparedStatement st=null; 5 ResultSet rs=null; 6 Boolean result=false; 7 try { 8 //获取连接 9 conn=nection();10 //编写SQL语句11 String adminLoginSql="select * from aduser where adname='"+admin+"' and password='"+password+"'";12 //创建语句执⾏者13 st=eStatement(adminLoginSql);14 //获取结果15 rs=eQuery();16 if(())17 result=true;18 else19 result=false;20 } catch (Exception e) {21 tackTrace();22 }23 finally {24 esource(conn, st, rs);25 }26 return result;27 }28 }

  ②biz层主要是业务逻辑的实现  AdminBiz类似Dao层,所以就不贴代码了   1 public class AdminBizImpl implements AdminBiz{ 2 private AdminDao adminDao; 3 public boolean adminLogin(String admin, String password) { 4 //实例化接⼝ 5 adminDao=new AdminDaoImpl(); 6 admin2=new (); 7 in(admin); 8 sword(password); 9 Boolean result=ogin(in(), sword());10 return result;11 }12 }

  ③utils是⼯具类,主要有数据库连接的类,这样每次操作数据库不⽤重写代码    ④pojo层是数据库表的映射实体类  类主要⽤于图书分页实现   1 public class PageBean { 2 private int currentPage=1;//当前页,默认显⽰第⼀页 3 private int pageCount=5;//每页显⽰的⾏数 4 private int totalCount;//总记录数 5 private int totalPage;//总页数=总记录数/每页显⽰的⾏数+1 6 private List pageData; 7

8 //获取总页数 9 public int getTotalPage() {10 if(totalCount%pageCount==0)11 totalPage=totalCount/pageCount;//被整除情况12 else {13 totalPage=totalCount/pageCount+1;14 }15 return totalPage;16 }17 public void setTotalPage(int totalPage) {18 age=totalPage;19 }20 public int getCurrentPage() {21 return currentPage;22 }23 public void setCurrentPage(int currentPage) {24 tPage = currentPage;25 }26 public int getPageCount() {27 return pageCount;28 }29 public void setPageCount(int pageCount) {30 unt = pageCount;31 }32 public int getTotalCount() {33 return totalCount;34 }35 public void setTotalCount(int totalCount) {36 ount = totalCount;37 }38 public List getPageData() {39 return pageData;40 }41 public void setPageData(List pageData) {42 ta = pageData;43 }44 }View Code

  ⑤servlet控制层  类(⽤于图书管理端分页等功能的实现)   1 public class AdminServlet extends HttpServlet{ 2 @Override 3 protected void service(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException { 4 int currentPage;//获得当前页 5 String current=(String) ameter("page"); 6 if(current==null) 7 currentPage=1; 8 else 9 currentPage=nt(current);10 //实例化业务层11 bookBiz bookbiz=new bookBizImpl();12 PageBean pb=new PageBean();13 alCount(alCount());14 if(currentPage>0)15 rentPage(currentPage);16 //分页查询17 ArrayList books=AllBook(pb);18 //获得总页数19 int pageCount=alPage();20 //将book集合和页⾯数跳转⾄页⾯21 ribute("bookslist", books);22 ribute("pagesize", pageCount);23 uestDispatcher("web/").forward(req, rep);24 }25 }View Code

  类(⽤于图书编辑修改等的实现)   1 public class BookServlet extends HttpServlet{ 2 @Override 3 protected void service(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException { 4 int bookid=nt(ameter("bookid")); 5 bookBiz bookbiz=new bookBizImpl(); 6 book bk=kById(bookid); 7 if(ameter("method")!=null&&ameter("method").equals("update")) { 8 int borrow=nt(ameter("borrow")); 9 String intro=ameter("intro");10 if(ok(bookid, intro, borrow)) {11 //ribute("bk", bk);12 //uestDispatcher("bookServlet?bookid="+bookid).forward(req, rep);13 PrintWriter out=ter();14 n("success");15 }16 }17 else {18 //将获取到的book数据发送并跳转⾄页⾯19 ribute("bk", bk);20 uestDispatcher("web/?bookid="+bookid).forward(req, rep);21 }22 }23 }View Code  类(⽤于登录过滤实现)  1 public class LogFilter implements Filter{ 2 private FilterConfig config; 3 @Override 4 public void destroy() { 5 // TODO Auto-generated method stub 6

7 } 8 /* 9 * Filter的核⼼处理10 * */11 @Override12 public void doFilter(ServletRequest req, ServletResponse rep, FilterChain filc)13 throws IOException, ServletException {14 HttpServletRequest request=(HttpServletRequest) req;15 HttpServletResponse response=(HttpServletResponse) rep;16 HttpSession session=sion();17 //如果session中有logined,则证明过滤成功18 if(ribute("logined")!=null) {19 er(req, rep);20 }21 else22 direct("web/");23 }24

25 @Override26 public void init(FilterConfig config) throws ServletException {27 =config;28 }29

30 }View Code

3.数据库设计  aduser表  4.前端JSP设计  ①页⾯  

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 4 5 6 7 管理员登录页⾯ 8 9 10 11 14 15

16

17
18

图书管理系统

19
20

21

22 23
24

25 placeholder="请输⼊⽤户名">26

27
28
29 30
31

32 placeholder="请输⼊密码">33

34
35
36
37 38
39
40

41

42
43
44 45 View Code

  ②页⾯(管理端)  

1 <%@page import="an"%> 2 <%@page import=""%> 3 <%@page import="ist"%> 4 <%@page import="zImpl"%> 5 <%@page import="z"%> 6 <%@ taglib uri="/jsp/jstl/core" prefix="c" %> 7 <%@ page language="java" contentType="text/html; charset=utf-8" 8 pageEncoding="utf-8"%> 9 10 11 12 13 管理页⾯ 14 15 16 17 18 19 20 21 22

23

24
25

26 图书系统管理员端 27

28
29
30
31 33
34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 56 59 70 73 76 79 84 85 86 87
书籍封⾯书籍名称是否借出书籍作者分类⽬录简介操作
52
53 54
55
57

${book['bk_name']}

58
60

61

62 63 64 65 66 67 68

69
71

${book['bk_name']}

72
74

${bk['bk_category']}

75
77

${book['bk_intro']}

78
80
81 82
83
88 95
96
97 100
101
102
103
104
105 106 View Code

  ③(图书编辑页⾯) 1 <%@ page language="java" contentType="text/html; charset=utf8" 2 pageEncoding="utf8"%> 3 <%@ taglib uri="/jsp/jstl/core" prefix="c" %> 4 5 6 7 8 编辑图书 9 10 11 12 13 14 32 33 34

35

36 管理员端37

38
39
40 41 42 43 44 45 46 47 53 54 55 56 57 58 59 60 63 64 65 66 67 68 69
书籍名称${bk['bk_name']}
是否借出48 52
书籍作者${bk['bk_author']}
书籍分类61 ${bk['bk_category']}62
书籍介绍
70
71
72
73 74 View Code

等配置⽂件

发布者:admin,转转请注明出处:http://www.yc00.com/news/1690579761a372616.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信