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
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
⑤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
类(⽤于图书编辑修改等的实现) 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 716
图书管理系统
19 42②页⾯(管理端)
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 1323
26 图书系统管理员端 27
28书籍封⾯ | 39书籍名称 | 40是否借出 | 41书籍作者 | 42分类⽬录 | 43简介 | 44操作 | 45
---|---|---|---|---|---|---|
52 53 54 55 | 56 57 ${book['bk_name']} 58 | 59 60 61 62 | 70 71 ${book['bk_name']} 72 | 73 74 ${bk['bk_category']} 75 | 76 77 ${book['bk_intro']} 78 | 79 80 81 82 83 | 84
- 89
- class="btn disabled">« 90
- class="btn disabled">
- class="btn disabled">» 94
③(图书编辑页⾯) 1 <%@ page language="java" contentType="text/html; charset=utf8" 2 pageEncoding="utf8"%> 3 <%@ taglib uri="/jsp/jstl/core" prefix="c" %> 4 5 6
7 836 管理员端37
38等配置⽂件
发布者:admin,转转请注明出处:http://www.yc00.com/news/1690579761a372616.html
评论列表(0条)