IDEA+Java+Servlet+JSP+Mysql实现Web图书管理系统【建议收藏】(下)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: IDEA+Java+Servlet+JSP+Mysql实现Web图书管理系统【建议收藏】

三、部分代码


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存储数据


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
24天前
|
关系型数据库 MySQL Java
【IDEA】java后台操作mysql数据库驱动常见错误解决方案
【IDEA】java后台操作mysql数据库驱动常见错误解决方案
49 0
|
24天前
|
Java 容器
【学习笔记】Jsp与Servlet技术
【学习笔记】Jsp与Servlet技术
55 0
|
25天前
|
SQL 存储 关系型数据库
MySQL 与 IntelliJ IDEA 同时兼容的注释
MySQL 与 IntelliJ IDEA 同时兼容的注释
13 2
|
24天前
|
数据可视化 关系型数据库 MySQL
【IDEA】配置mysql环境并创建mysql数据库
【IDEA】配置mysql环境并创建mysql数据库
53 0
|
3月前
|
存储 缓存 前端开发
Servlet与JSP在Java Web应用中的性能调优策略
Servlet与JSP在Java Web应用中的性能调优策略
30 1
|
3月前
|
Java Maven Android开发
解锁Web开发新技能:从零开始的Struts 2之旅——让你的Java编程之路更加宽广,首个应用实例带你飞!
【8月更文挑战第31天】对于初学者,掌握 Struts 2 框架不仅能提升 Web 开发能力,还能深入了解 MVC 架构。Struts 2 是一个基于 Servlet 的 Java 框架,提供表单验证、文件上传、国际化等功能,便于快速构建易维护的 Web 应用。本文通过示例演示如何从零开始搭建环境并创建一个简单的 Struts 2 项目,包括配置 `struts.xml`、编写 Action 类及视图文件,并配置 web.xml。通过这些步骤,你将学会基本的开发流程,为进一步学习高级功能打下基础。
45 0
|
3月前
|
存储 Java 关系型数据库
基于Servlet和JSP的Java Web应用开发指南
基于Servlet和JSP的Java Web应用开发指南
36 0
|
3月前
|
前端开发 安全 Java
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
25 0
|
3月前
|
应用服务中间件
2022年最新最详细在IDEA中配置Tomcat(含有详细图解过程)、建立使用IEDA建立一个Web项目的案例
这篇文章提供了在IntelliJ IDEA中配置Tomcat服务器的详细步骤,包括添加Tomcat Server、选择安装路径、添加项目Artifact,以及创建和展示Web项目的流程。
|
16天前
|
存储 关系型数据库 MySQL
Mysql(4)—数据库索引
数据库索引是用于提高数据检索效率的数据结构,类似于书籍中的索引。它允许用户快速找到数据,而无需扫描整个表。MySQL中的索引可以显著提升查询速度,使数据库操作更加高效。索引的发展经历了从无索引、简单索引到B-树、哈希索引、位图索引、全文索引等多个阶段。
50 3
Mysql(4)—数据库索引