荐 使用IDEA搭建一个简单的JavaWeb图书管理项目(详细步骤指导、提供源码)
写在前边:
这个一个JavaWeb项目的基础实战的分享,当然如果可以你们可以拿走当作业(前提你们的作业要求并不高)
本项目只实现了基本的增删改查,略懂Java基础也可以拿走根据需要修改,也算提供了一个项目结构组织的框架,可以作为参考。本项目暂时只实现了基础的增删改查一套,模糊查询一个,还有分页查询。这些内容也算是开发JavaWeb项目开发的核心内容,其它的可以根据需要自己完善。
源码下载 ↓ ↓ ↓
项目压缩包下载 — 百度网盘 提取码: 9vh7
项目压缩包下载 — CSDN
GitHub下载
目录
一、开发环境和项目结构
开发环境
- JDK 8
- MySQL 5.7
- Tomcat 9.0
- IDEA 2018
- 推荐使用当前或更高开发环境
项目结构
二、项目配置
emmmm,有点长我单独写了一篇博客,需要的可以去看一下
https://blog.csdn.net/weixin_44580492/article/details/107335967
三、代码编写步骤
- 开始编写之前,推荐将项目结构中的 包 先构建好
- 1~6步属于C/S项目
- 1~9步属于B/S项目,第6步除外,可省略不写
1.编写数据库代码
创建名为 book 的数据库
create database book;
创建名为 book 的数据表
CREATE TABLE `book` (
`BOOK_ID` int(50) NOT NULL AUTO_INCREMENT,
`BOOK_NAME` varchar(100) NOT NULL,
`ISBN` varchar(100),
`CATEGORY` varchar(100),
PRIMARY KEY (`BOOK_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.编写工具类
在util包下操作
编写数据库连接配置文件 db-config.properties
jdbc.url=jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
编写数据库连接类 DBUtil
package com.jmp.util;
import java.sql.*;
import java.util.ResourceBundle;
/**
* @author JYH
* @version 1.0
* @date 2020/6/19 23:53
*/
public class DBUtil {
//数据库连接地址
private static final String URL;
//数据库用户名
private static final String USERNAME;
//数据库密码
private static final String PASSWORD;
//加载数据库连接驱动程序
private static final String DRIVER;
private DBUtil(){}
//加载数据库配置文件
private static ResourceBundle rb = ResourceBundle.getBundle("com.jmp.util.db-config");
//加载数据库配置文件中的连接信息
static{
URL = rb.getString("jdbc.url");
USERNAME = rb.getString("jdbc.username");
PASSWORD = rb.getString("jdbc.password");
DRIVER = rb.getString("jdbc.driver");
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//创建数据库连接
public static Connection getConnection() {
Connection con = null;
try {
con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
// System.out.println("数据库连接成功。");
} catch (SQLException throwables) {
throwables.printStackTrace();
System.out.println("数据库连接失败,请检查数据库地址、用户名、密码是否存在错误!!!");
}
return con;
}
//关闭数据库连接
public static void close(ResultSet rs, Statement stat,Connection con) throws SQLException {
if(rs != null){
rs.close();
}if(stat != null){
rs.close();
}if(con != null){
rs.close();
}
System.out.println("数据库连接已关闭!");
}
//测试数据库连接
public static void main(String[] args) {
System.out.println(DBUtil.getConnection());
}
}
编写完DBUtil类后我们可以先运行一下 main 方法,看数据库能否连接成功。
编写分页工具类 PageBean
package com.jmp.util;
/**
* 分页工具类
* @author JYH
* @version 1.0
* @date 2020/6/23 11:42
*/
public class PageBean {
private int page;// 页码
private int rows;// 页大小
private int total;// 总记录数
private boolean pagination = true;// 是否分页
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
}
3.编写实体类
在pojo包下操作
编写图书实体类 Book
package com.jmp.pojo;
/**
* @author JYH
* @version 1.0
* @date 2020/6/19 23:50
*/
public class Book {
private int book_id;
private String book_name;
private String isbn;
private String category;
public Book() {
}
public Book(int book_id, String book_name, String isbn, String category) {
this.book_id = book_id;
this.book_name = book_name;
this.isbn = isbn;
this.category = category;
}
public int getBook_id() {
return book_id;
}
public void setBook_id(int book_id) {
this.book_id = book_id;
}
public String getBook_name() {
return book_name;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public String toString() {
return "Book{" +
"book_id=" + book_id +
", book_name='" + book_name + '\'' +
", isbn='" + isbn + '\'' +
", category='" + category + '\'' +
'}';
}
}
4.编写数据处理层
在dao包下操作
创建接口 BookDao,规定需要实现的功能
package com.jmp.dao;
import com.jmp.pojo.Book;
import java.sql.SQLException;
import java.util.List;
/**
* @author JYH
* @version 1.0
* @date 2020/6/20 0:21
*/
public interface BookDao {
/**
* 查询所有图书
*/
List<Book> selectAll() throws SQLException;
/**
* 添加图书
*/
void add(Book book) throws SQLException;
/**
* 更新图书信息
*/
void update(Book book) throws SQLException;
/**
* 删除图书
*/
void delete(Book book) throws SQLException;
/**
* 根据书名模糊查询
*/
List<Book> blurryName(Book book) throws SQLException;
/**
* 获取具体的图书信息
* @return
*/
Book bookInfo(Book book) throws SQLException;
/**
* 分页查询图书
*/
List<Book> selectAll(int currentPage,int pageSize) throws SQLException;
}
根据需要实现的功能,编写对应的数据库代码配置文件 book-sql.properties
book.sql.selectAll=select * from book
book.sql.add=insert into book(book_name,isbn,category) value (?,?,?)
book.sql.update=update book set book_name=?,isbn=?,category=? where book_id=?
book.sql.delete=delete from book where book_id=?
book.sql.blurryName=select * from book where book_name like ?
book.sql.deleteBook=select * from book where book_id=?
book.sql.selectAllPage=select * from book limit ?,?
编写 BookDao 接口的实现类 BookDaoImpl
package com.jmp.dao;
import com.jmp.pojo.Book;
import com.jmp.util.DBUtil;
import lombok.Data;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;
/**
* @author JYH
* @version 1.0
* @date 2020/6/20 0:41
*/
@Data
public class BookDaoImpl implements BookDao{
//查询运行器
static QueryRunner qr = null;
//加载sql文件
static ResourceBundle rb = ResourceBundle.getBundle("com.jmp.dao.book-sql");
public BookDaoImpl(){
//创建BookDaoImpl对象时加载QueryRunner
qr = new QueryRunner();
}
@Override
public List<Book> selectAll() throws SQLException {
String sql = rb.getString("book.sql.selectAll");
List<Book> list = qr.query(DBUtil.getConnection(),sql,new BeanListHandler<Book>(Book.class));
return list;
}
@Override
public void add(Book book) throws SQLException {
String sql = rb.getString("book.sql.add");
qr.update(DBUtil.getConnection(),sql,book.getBook_name(),book.getIsbn(),book.getCategory());
}
@Override
public void update(Book book) throws SQLException {
String sql = rb.getString("book.sql.update");
qr.update(DBUtil.getConnection(),sql,book.getBook_name(),book.getIsbn(),book.getCategory(),book.getBook_id());
}
@Override
public void delete(Book book) throws SQLException {
String sql = rb.getString("book.sql.delete");
qr.update(DBUtil.getConnection(),sql,book.getBook_id());
}
@Override
public List<Book> blurryName(Book book) throws SQLException {
String sql = rb.getString("book.sql.blurryName");
List<Book> list = qr.query(DBUtil.getConnection(),sql,new BeanListHandler<Book>(Book.class),"%"+book.getBook_name()+"%");
return list;
}
@Override
public Book bookInfo(Book book) throws SQLException{
String sql = rb.getString("book.sql.deleteBook");
Book b = qr.query(DBUtil.getConnection(),sql,new BeanHandler<Book>(Book.class),book.getBook_id());
return b;
}
@Override
public List<Book> selectAll(int currentPage, int pageSize) throws SQLException {
String sql = rb.getString("book.sql.selectAllPage");
List<Book> list = qr.query(DBUtil.getConnection(),sql,new BeanListHandler<>(Book.class),((currentPage-1)*pageSize),pageSize);
return list;
}
}
5.编写业务逻辑层
在service包下操作
编写 BookService 接口,规定需要实现的业务
package com.jmp.service;
import com.jmp.pojo.Book;
import java.sql.SQLException;
import java.util.List;
/**
* @author JYH
* @version 1.0
* @date 2020/6/20 1:36
*/
public interface BookService {
/**
* 查询所有图书
*/
List<Book> selectAll() throws SQLException;
/**
* 添加图书
*/
void add(Book book) throws SQLException;
/**
* 更新图书信息
*/
void update(Book book) throws SQLException;
/**
* 删除图书
*/
void delete(Book book) throws SQLException;
/**
* 根据书名模糊查询
*/
List<Book> blurryName(Book book) throws SQLException;
/**
* 获取删除图书的信息
* @return
*/
Book bookInfo(Book book) throws SQLException;
/**
* 分页查询图书(后期添加 --- 使用default关键字)
*/
List<Book> selectAll(int currentPage,int pageSize) throws SQLException;
}
编写 BookService 接口的实现类 BookServiceImpl
package com.jmp.service;
import com.jmp.dao.BookDao;
import com.jmp.dao.BookDaoImpl;
import com.jmp.pojo.Book;
import lombok.Data;
import java.sql.SQLException;
import java.util.List;
/**
* @author JYH
* @version 1.0
* @date 2020/6/20 1:37
*/
@Data
public class BookServiceImpl implements BookService{
BookDao bookDao = new BookDaoImpl();
@Override
public List<Book> selectAll() throws SQLException {
return bookDao.selectAll();
}
@Override
public void add(Book book) throws SQLException {
bookDao.add(book);
}
@Override
public void update(Book book) throws SQLException {
bookDao.update(book);
}
@Override
public void delete(Book book) throws SQLException {
bookDao.delete(book);
}
@Override
public List<Book> blurryName(Book book) throws SQLException {
return bookDao.blurryName(book);
}
@Override
public Book bookInfo(Book book) throws SQLException{
return bookDao.bookInfo(book);
}
@Override
public List<Book> selectAll(int currentPage, int pageSize) throws SQLException {
return bookDao.selectAll(currentPage,pageSize);
}
}
6.编写控制台程序(C/S)
在view包下操作
编写 BookManager 类,在控制台使用图书管理系统的功能
package com.jmp.view;
import com.jmp.pojo.Book;
import com.jmp.service.BookService;
import com.jmp.service.BookServiceImpl;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
/**
* @author JYH
* @version 1.0
* @date 2020/6/20 1:47
*/
public class BookManager {
static Scanner sc = new Scanner(System.in);
static BookService bookService = new BookServiceImpl();
public static void main(String[] args) {
while (true) {
System.out.println("|----------------图书管理系统-----------------|");
System.out.println("|1-增加 2-删除 3-修改 4-查找 5-显示所有 0-退出|");
System.out.println("|--------------------------------------|");
System.out.println("请选择业务");
int x = sc.nextInt();
switch (x) {
case 1:
add();
break;
case 2:
delete();
break;
case 3:
update();
break;
case 4:
bookLike();
break;
case 5:
selectAll();
break;
case 0:
System.out.println("程序已退出,欢迎再次使用!");
System.exit(0);
break;
default:
System.out.println("输入数据有误,请重新输入!");
break;
}
}
}
public static void add(){
System.out.println("------------增加------------");
System.out.println("图书名称:");
String name = sc.next();
System.out.println("ISBN编号:");
String isbn = sc.next();
System.out.println("图书类别:");
String category = sc.next();
Book book = new Book(0,name,isbn,category);
try {
bookService.add(book);
System.out.println("增加成功!---------------\n");
} catch (SQLException e1) {
e1.printStackTrace();
}
}
public static void delete() {
System.out.println("------------删除------------");
System.out.println("请输入要删除的图书编号:");
int id = sc.nextInt();
Book book = new Book();
book.setBook_id(id);
try {
book = bookService.bookInfo(book);
System.out.println(book.toString());
System.out.println("确认要删除吗?1-是 0-否");
int cmd = sc.nextInt();
if(1==cmd) {
bookService.delete(book);
System.out.println("删除成功!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void update() {
System.out.println("------------修改------------");
System.out.println("请输入要修改图书的编号:");
int id = sc.nextInt();
Book book = new Book();
book.setBook_id(id);
try {
book = bookService.bookInfo(book);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(book.toString());
System.out.println("请重新输入图书的信息:");
System.out.println("图书名称:");
String name = sc.next();
System.out.println("ISBN编号:");
String isbn = sc.next();
System.out.println("图书类别:");
String category = sc.next();
book = new Book(id,name,isbn,category);
try {
bookService.update(book);
System.out.println("修改成功!---------------");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void bookLike() {
System.out.println("-------------模糊查询------------");
System.out.println("请输入图书名称关键字:");
String name = sc.next();
Book book = new Book();
book.setBook_name(name);
try {
List<Book> list = bookService.blurryName(book);
for(Book b:list) {
System.out.println(b.toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void selectAll() {
System.out.println("-------------查询所有------------");
try {
List<Book> list = bookService.selectAll();
for(Book book:list) {
System.out.println(book.toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
7.编写业务控制层(B/S)
在servlet包下操作
编写 BookServlet,对整体业务进行控制
package com.jmp.servlet;
import com.jmp.pojo.Book;
import com.jmp.service.BookService;
import com.jmp.service.BookServiceImpl;
import com.jmp.util.PageBean;
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 java.io.IOException;
import java.sql.SQLException;
import java.util.List;
/**
* @author JYH
* @version 1.0
* @date 2020/6/20 22:10
*/
@WebServlet(name = "BookServlet",urlPatterns = "/BookServlet")
public class BookServlet extends HttpServlet {
BookService bookService = new BookServiceImpl();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String m = request.getParameter("m");
if ("add".equals(m)){
add(request,response);
}else if("update".equals(m)){
update(request,response);
}else if("delete".equals(m)){
delete(request,response);
}else if("selectAll".equals(m)){
selectAll(request,response);
}else if("bookInfo".equals(m)){
bookInfo(request,response);
}else if ("blurryname".equals(m)){
bookBlurryName(request, response);
}
}
private void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String bookName = request.getParameter("book_name");
String isbn = request.getParameter("isbn");
String category = request.getParameter("category");
Book book = new Book(0,bookName,isbn,category);
System.out.println(book.toString());
try {
bookService.add(book);
selectAll(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
private void update(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String bookId = request.getParameter("book_id");
String bookName = request.getParameter("book_name");
String isbn = request.getParameter("isbn");
String category = request.getParameter("category");
Book book = new Book(Integer.parseInt(bookId),bookName,isbn,category);
try {
bookService.update(book);
selectAll(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
private void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
String bookId = request.getParameter("book_id");
Book book = new Book();
book.setBook_id(Integer.parseInt(bookId));
try {
bookService.delete(book);
selectAll(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
PageBean pb = new PageBean();
int pageUp = pb.getPage();
int pageDown = pb.getPage();
private void selectAll(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
if("pageUp".equals(request.getParameter("pageUp"))){
System.out.println(request.getParameter("pageUp"));
pb.setPage(pb.getPage()-1);
// request.setAttribute("pageUp",pb.getPage());
if (pb.getPage() < 1){
pb.setPage(1);
}
try {
List<Book> booklist = bookService.selectAll(pb.getPage(),5);
request.setAttribute("bookList",booklist);
request.getRequestDispatcher("/views/booklist.jsp").forward(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}else if("pageDown".equals(request.getParameter("pageDown"))){
System.out.println(request.getParameter("pageDown"));
pb.setPage(pb.getPage()+1);
// request.setAttribute("pageDown",pb.getPage());
try {
List<Book> booklist = bookService.selectAll(pb.getPage(),5);
request.setAttribute("bookList",booklist);
if ("".equals(booklist)){
request.setAttribute("null_err","<h3>查询内容为空请返回上一页</h3>");
}
request.getRequestDispatcher("/views/booklist.jsp").forward(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}else{
// request.setAttribute("pageUp",1);
// request.setAttribute("pageDown",2);
pb.setPage(1);
try {
List<Book> booklist = bookService.selectAll(pb.getPage(),5);
request.setAttribute("bookList",booklist);
request.getRequestDispatcher("/views/booklist.jsp").forward(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/* private void selectAll(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
try {
List<Book> booklist = bookService.selectAll(1,5);
request.setAttribute("bookList",booklist);
request.getRequestDispatcher("/views/booklist.jsp").forward(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}*/
private void bookInfo(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String bookId = request.getParameter("book_id");
Book book = new Book();
book.setBook_id(Integer.parseInt(bookId));
try {
Book b = bookService.bookInfo(book);
request.setAttribute("b", b);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
request.getRequestDispatcher("/views/updatebook.jsp").forward(request, response);
}
private void bookBlurryName(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
String keyName = request.getParameter("blurryname");
Book book = new Book();
book.setBook_name(keyName);
try {
List<Book> bookblurryList = bookService.blurryName(book);
request.setAttribute("bookblurryList",bookblurryList);
request.getRequestDispatcher("/views/blurrynamebook.jsp").forward(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
8.编写过滤器
在filter包下操作
编写字符编码过滤器 CharacterEncodingFilter,防止数据传递和页面乱码
package com.jmp.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;
/**
* @author JYH
* @version 1.0
* @date 2020/6/21 0:15
*/
@WebFilter(
filterName="CharacterEncodingFilter",
urlPatterns="/*",
initParams= {@WebInitParam(name="characterEncoding",value="utf-8")}
)
public class CharacterEncodingFilter implements Filter {
private String characterEncoding;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("characterEncoding="+characterEncoding);
if(null!=characterEncoding) {
req.setCharacterEncoding(characterEncoding);
resp.setCharacterEncoding(characterEncoding);
}
chain.doFilter(req, resp);
}
@Override
public void init(FilterConfig config) throws ServletException {
characterEncoding = config.getInitParameter("characterEncoding");
}
}
9.编写前端页面
在 web/views 文件夹下操作
编写书籍添加页面,addbook.jsp
<%--
Created by IntelliJ IDEA.
User: JYH
Date: 2020/6/20
Time: 20:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加图书</title>
</head>
<body>
<div align="center">
<h3>添加图书</h3>
<form action="${pageContext.request.contextPath}/BookServlet?m=add" method="post">
<table>
<tr>
<td>图书名称</td>
<td><input type="text" name="book_name"></td>
</tr>
<tr>
<td>ISBN编号</td>
<td><input type="text" name="isbn"></td>
</tr>
<tr>
<td>图书类型</td>
<td><input type="text" name="category"></td>
</tr>
</table>
<input type="submit" value="添加"> <input type="reset" value="重置">
</form>
</div>
</body>
</html>
编写书籍模糊查询页面,blurrynamebook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: JYH
Date: 2020/6/21
Time: 0:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书名模糊查询</title>
<style>
table,table tr th,table tr td{
border:1px solid #000;
text-align:center;
}
table{
width:600px;
border-collapse:collapse;
}
#index p{
padding-top:5px;
padding-left:480px;
}
a{
text-decoration:none;
}
</style>
</head>
<body>
<div align="center">
<h3>书名模糊查询</h3>
<form action="${pageContext.request.contextPath}/BookServlet?m=blurryname" method="post">
<input type="text" name="blurryname"><input type="submit" value="搜索"><br><br>
<div id="index"><a href="/index.jsp">返回首页</a></div>
<table>
<tr>
<td>图书编号</td>
<td>图书名称</td>
<td>ISBN编号</td>
<td>图书类别</td>
<td>选择操作</td>
</tr>
<c:forEach items="${bookblurryList}" var="bookblurrylist">
<tr>
<td>${bookblurrylist.book_id}</td>
<td>${bookblurrylist.book_name}</td>
<td>${bookblurrylist.isbn}</td>
<td>${bookblurrylist.category}</td>
<td>
<a href="${pageContext.request.contextPath}/BookServlet?m=bookInfo&book_id=${bookblurrylist.book_id}">修改</a>
<a href="${pageContext.request.contextPath}/BookServlet?m=delete&book_id=${bookblurrylist.book_id}" onclick="return confirm('您确定要删除吗?');">删除</a>
</td>
</tr>
</c:forEach>
</form>
<br>
</div>
</body>
</html>
编写书籍列表页面,booklist.jsp
<%--
Created by IntelliJ IDEA.
User: JYH
Date: 2020/6/20
Time: 21:48
To change this tbooklate use File | Settings | File Tbooklates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>图书列表</title>
<style>
table,table tr th,table tr td{
border:1px solid #000;
text-align:center;
}
table{
width:600px;
border-collapse:collapse;
}
#addbook{
width:600px;
height:50px;
}
#addbook p{
padding-top:5px;
padding-left:480px;
}
a{
text-decoration:none;
}
</style>
</head>
<body>
<div align="center">
<h3>所有图书列表</h3>
<div id="addbook">
<p><a href="${pageContext.request.contextPath}/views/addbook.jsp">添加图书</a></p>
<p><a href="${pageContext.request.contextPath}/views/blurrynamebook.jsp">书名关键字查询</a></p>
</div>
<br><br>
<table>
<tr>
<td>图书编号</td>
<td>图书名称</td>
<td>ISBN编号</td>
<td>图书类别</td>
<td>选择操作</td>
</tr>
<c:forEach items="${bookList}" var="booklist">
<tr>
<td>${booklist.book_id}</td>
<td>${booklist.book_name}</td>
<td>${booklist.isbn}</td>
<td>${booklist.category}</td>
<td>
<a href="${pageContext.request.contextPath}/BookServlet?m=bookInfo&book_id=${booklist.book_id}">修改</a>
<a href="${pageContext.request.contextPath}/BookServlet?m=delete&book_id=${booklist.book_id}" onclick="return confirm('您确定要删除吗?');">删除</a>
</td>
</tr>
</c:forEach>
</table>
${null_err}
<div>
<a href="${pageContext.request.contextPath}/BookServlet?m=selectAll&pageUp=pageUp">上一页</a>
<a href="${pageContext.request.contextPath}/BookServlet?m=selectAll&pageDown=pageDown">下一页</a>
</div>
</div>
</body>
</html>
编写书籍更新页面,updatebook.jsp
<%--
Created by IntelliJ IDEA.
User: JYH
Date: 2020/6/20
Time: 21:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>更新图书</title>
</head>
<body>
<div align="center">
<h3>修改图书</h3>
<form action="${pageContext.request.contextPath}/BookServlet?m=update" method="post">
<table>
<tr>
<td>图书编号</td>
<td><input type="text" name="book_id" value="${b.book_id}" readonly="readonly"></td>
</tr>
<tr>
<td>图书名称</td>
<td><input type="text" name="book_name" value="${b.book_name}"></td>
</tr>
<tr>
<td>ISBN编号</td>
<td><input type="text" name="isbn" value="${b.isbn}"></td>
</tr>
<tr>
<td>图书类别</td>
<td><input type="text" name="category" value="${b.category}"></td>
</tr>
</table>
<input type="submit" value="修改"> <input type="reset" value="重置">
</form>
</div>
</body>
</html>
在 web 文件夹下操作
更改 index.jsp 代码,完善程序入口
<%-- Created by IntelliJ IDEA. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎使用图书管理系统</title>
</head>
<body>
<div align="center">
<h3><a href="views/addbook.jsp">添加图书</a></h3>
<h3><a href="BookServlet?m=selectAll">查询所有图书</a></h3>
<h3><a href="views/blurrynamebook.jsp">图书名称关键字查询图书</a></h3>
<%--<h3><a href="views/updatebook.jsp">更新图书</a></h3>--%>
</div>
</body>
</html>
本文地址:https://blog.csdn.net/weixin_44580492/article/details/106933749