图书管理系统

2021-10-15 15:31:03 浏览数 (1)

需求

进入登录页面

登录成功跳转至列表页面否则提示账号密码错误并返回登录页面

列表页面可以新增图书、查询图书、点击图书名称跳转至详情页面、有修改逻辑和删除逻辑

分析及代码实现

分层结构图

注意:需要导入.jar文件!!!

注意:需要导入.jar文件!!!

注意:需要导入.jar文件!!!

创建一个util工具包

DBUtil工具类

代码语言:javascript复制
package top.gaojc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {

	static {
		// 1.添加驱动程序包
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConn() {
		// 2.连接数据库
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=library",
					"sa", "1");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
		// 3.关闭资源
		try {
			if (conn != null)
				conn.close();

			if (ps != null)
				ps.close();

			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static int update(String sql, Object[] objs) {
		// 4.更新操作
		Connection conn = getConn();
		PreparedStatement ps = null;
		int count = 0;
		try {
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < objs.length; i  ) {
				ps.setObject(i   1, objs[i]);
			}
			count = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, null);
		}
		return count;
	}
}

创建一个bean包用来实现代码的重复利用

Books.java(图书信息)

代码语言:javascript复制
package top.gaojc.bean;

public class Books {
	
	private int id;
	private String bookName;
	private String author;
	private float price;
	private String ISBN;
	private String press;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public String getISBN() {
		return ISBN;
	}
	public void setISBN(String iSBN) {
		ISBN = iSBN;
	}
	public String getPress() {
		return press;
	}
	public void setPress(String press) {
		this.press = press;
	}
	
	public Books() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Books(int id, String bookName, String author, float price,
			String iSBN, String press) {
		super();
		this.id = id;
		this.bookName = bookName;
		this.author = author;
		this.price = price;
		ISBN = iSBN;
		this.press = press;
	}
	
	@Override
	public String toString() {
		return "Books [ISBN="   ISBN   ", author="   author   ", bookName="
				  bookName   ", id="   id   ", press="   press   ", price="
				  price   "]";
	}
}

Users.java(用来登录的用户信息)

代码语言:javascript复制
package top.gaojc.bean;

public class Users {
	
	private int id;
	private String userName;
	private String pwd;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public Users(int id, String userName, String pwd) {
		super();
		this.id = id;
		this.userName = userName;
		this.pwd = pwd;
	}
	
	public Users() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public String toString() {
		return "Users [id="   id   ", pwd="   pwd   ", userName="   userName
				  "]";
	}
}

创建一个dao接口包

LoginDao.java

代码语言:javascript复制
package top.gaojc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import top.gaojc.bean.Books;
import top.gaojc.bean.Users;
import top.gaojc.util.DBUtil;

public class LoginDao {

	// 登录业务
	public static Users login(String userName, String pwd) {
		Users user = null;
		Connection conn = DBUtil.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from users where userName=? and pwd=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, pwd);
			rs = ps.executeQuery();
			if (rs.next()) {
				user = new Users();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("userName"));
				user.setPwd(rs.getString("pwd"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	// 查询所有图书信息
	public static ArrayList<Books> list() {
		ArrayList<Books> bookList = new ArrayList<Books>();
		Connection conn = DBUtil.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Books books = null;
		String sql = "select * from books";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				books = new Books();
				books.setId(rs.getInt("id"));
				books.setBookName(rs.getString("bookName"));
				books.setAuthor(rs.getString("author"));
				books.setPrice(rs.getFloat("price"));
				books.setISBN(rs.getString("ISBN"));
				books.setPress(rs.getString("press"));
				bookList.add(books);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bookList;
	}

	// 根据id获取图书信息
	public static Books updateBookByID(int id) {
		Connection conn = DBUtil.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Books books = null;
		String sql = "select * from books where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				books = new Books();
				books.setId(rs.getInt("id"));
				books.setBookName(rs.getString("bookName"));
				books.setAuthor(rs.getString("author"));
				books.setPrice(rs.getFloat("price"));
				books.setISBN(rs.getString("ISBN"));
				books.setPress(rs.getString("press"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return books;
	}

	// 根据图书名称获取图书信息
	public static Books infoByBookName(String bookName) {
		Connection conn = DBUtil.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Books books = null;
		String sql = "select * from books where bookName=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, bookName);
			rs = ps.executeQuery();
			if (rs.next()) {
				books = new Books();
				books.setId(rs.getInt("id"));
				books.setBookName(rs.getString("bookName"));
				books.setAuthor(rs.getString("author"));
				books.setPrice(rs.getFloat("price"));
				books.setISBN(rs.getString("ISBN"));
				books.setPress(rs.getString("press"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return books;
	}
}

创建一个action功能包

登录逻辑

LoginServlet.java代码
代码语言:javascript复制
package top.gaojc.action;

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 top.gaojc.bean.Books;
import top.gaojc.bean.Users;
import top.gaojc.dao.LoginDao;

public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 1:设置编码问题
		request.setCharacterEncoding("utf-8");

		// 2:获取参数
		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		System.out.println(userName);
		System.out.println(pwd);

		// 登录
		Users user = LoginDao.login(userName, pwd);

		// 3:响应
		if (user != null) {
			// 登录成功跳转到列表页面
			response.sendRedirect("ListServlet");
		} else {
			// 否则返回登录页面
			request.setAttribute("msg", "账号密码错误!");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
}
login.jsp
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()   "://"
			  request.getServerName()   ":"   request.getServerPort()
			  path   "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>登录页面</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<style type="text/css">
.top {
	text-align: center;
	color: red;
	font-size: 40px;
}
tr,td {
	text-align: center;
}
</style>
	</head>

	<body>
		<form action="LoginServlet" method="post">
			<table border="1" cellspacing="0" cellpadding="1" align="center">
				<div class="top">
					<span>登录页面</span>
				</div>
				<tr>
					<td>
						<span>用户名:</span>
					</td>
					<td>
						<input type="text" name="userName" />
					</td>
				</tr>
				<tr>
					<td>
						<span>密码:</span>
					</td>
					<td>
						<input type="password" name="pwd" />
						<span>${msg }</span>
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<input type="submit" value="登录" />
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

列表逻辑

ListServlet.java代码
代码语言:javascript复制
package top.gaojc.action;

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 top.gaojc.bean.Books;
import top.gaojc.dao.LoginDao;

public class ListServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1:设置编码 不需要设置编码

		// 2:获取参数 无参数

		// 3:响应
		ArrayList<Books> booksList = LoginDao.list();
		// 在这个地方我们可以对所有的图书信息中的某些信息进行特殊处理然后发送给页面进行显示
		request.setAttribute("booksList", booksList);
		// 转发到图书列表页面 看看有没有新增成功
		// 在当前代码中获取所有图书信息并通过request进行传递
		request.getRequestDispatcher("booksList.jsp").forward(request, response);
	}
}
booksList.jsp
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="top.gaojc.bean.Books"%>

<!-- JSP页面引入JSTL标签库,使用指令元素taglib-->
<%@taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme()   "://"
			  request.getServerName()   ":"   request.getServerPort()
			  path   "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>列表页面</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<style type="text/css">
.top {
	text-align: center;
	color: red;
	font-size: 40px;
	margin-left: 200px;
}
tr,td {
	text-align: center;
}
a {
	text-decoration: none;
	color: black;
}
</style>
	</head>

	<body>
		<form action="InfoBookNameServlet" method="post">
			<div class="top">
				<span>图书列表</span>
				<input type="text" name="bookName">
				<input type="submit" value="搜索">
			</div>
		</form>
		<table border="1" cellspacing="0" cellpadding="1" align="center"
			width="800px">
			<tr style="float: left">
				<td colspan="6">
					<a href="reg.jsp"><input type="button" value="新增图书" /> </a>
				</td>
			</tr>
			<tr>
				<td>
					<span>编号</span>
				</td>
				<td>
					<span>图书名称</span>
				</td>
				<td>
					<span>作者</span>
				</td>
				<td>
					<span>定价</span>
				</td>
				<td>
					<span>ISBN</span>
				</td>
				<td>
					<span>出版社</span>
				</td>
				<td>
					<span>操作</span>
				</td>
			</tr>

			<C:forEach items="${booksList}" var="booksList">
				<tr>
					<td>
						<span>${booksList.id}</span>
					</td>
					<td>
						<a href="InfoIdServlet?id=${booksList.id}">${booksList.bookName}</a>
					</td>

					<td>
						<span>${booksList.author}</span>
					</td>
					<td>
						<span>${booksList.price}</span>
					</td>
					<td>
						<span>${booksList.ISBN}</span>
					</td>
					<td>
						<span>${booksList.press}</span>
					</td>
					<td>
						<a href="UpServlet?id=${booksList.id}"><input type="submit"
								value="修改" /> </a>&nbsp;
						<a href="DeleteServlet?id=${booksList.id}"><input
								type="submit" value="删除" /> </a>
					</td>
				</tr>
			</C:forEach>
		</table>
	</body>
</html>

新增逻辑

RegServlet.java代码
代码语言:javascript复制
package top.gaojc.action;

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 top.gaojc.util.DBUtil;

public class RegServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 1:设置编码问题
		request.setCharacterEncoding("utf-8");

		// 2:处理请求
		String bookName = request.getParameter("bookName");
		String author = request.getParameter("author");
		String priceStr = request.getParameter("price");
		String ISBN = request.getParameter("ISBN");
		String press = request.getParameter("press");
		float price = Float.parseFloat(priceStr);

		// 3:添加sql语句
		String sql = "insert into books values(?,?,?,?,?)";
		Object[] objs = { bookName, author, price, ISBN, press };
		int count = DBUtil.update(sql, objs);

		// 4:处理响应
		if (count > 0) {
			response.sendRedirect("ListServlet");
		} else {
			response.sendRedirect("reg.jsp");
		}
	}
}
reg.jsp
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()   "://"
			  request.getServerName()   ":"   request.getServerPort()
			  path   "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>新增页面</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<style type="text/css">
.top {
	text-align: center;
	color: red;
	font-size: 40px;
}
tr,td {
	text-align: center;
}
</style>
	</head>

	<body>
		<form action="RegServlet" method="post">
			<table border="1" cellspacing="0" cellpadding="1" align="center">
				<div class="top">
					<span>新增页面</span>
				</div>
				<tr>
					<td>
						<span>图书名称:</span>
					</td>
					<td>
						<input type="text" name="bookName" />
					</td>
				</tr>
				<tr>
					<td>
						<span>作者:</span>
					</td>
					<td>
						<input type="text" name="author" />
					</td>
				</tr>
				<tr>
					<td>
						<span>定价:</span>
					</td>
					<td>
						<input type="text" name="price" />
					</td>
				</tr>
				<tr>
					<td>
						<span>ISBN:</span>
					</td>
					<td>
						<input type="text" name="ISBN" />
					</td>
				</tr>
				<tr>
					<td>
						<span>出版社:</span>
					</td>
					<td>
						<input type="text" name="press" />
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<input type="submit" value="添加" />
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

修改逻辑

UpServlet.java代码
代码语言:javascript复制
package top.gaojc.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import top.gaojc.bean.Books;
import top.gaojc.dao.LoginDao;

public class UpServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 1:设置编码
		request.setCharacterEncoding("utf-8");

		// 2:获取请求中的参数
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);

		// 调用方法
		Books books = LoginDao.updateBookByID(id);
		// 页面跳转
		request.setAttribute("books", books);
		request.getRequestDispatcher("update.jsp").forward(request, response);
	}
}
UpdateBookServlet.java代码
代码语言:javascript复制
package top.gaojc.action;

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 top.gaojc.util.DBUtil;

public class UpdateBookServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 1:设置编码
		request.setCharacterEncoding("utf-8");

		// 2:获取请求中的参数
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		String bookName = request.getParameter("bookName");
		String author = request.getParameter("author");
		String priceStr = request.getParameter("price");
		String ISBN = request.getParameter("ISBN");
		String press = request.getParameter("press");
		float price = Float.parseFloat(priceStr);

		// 注册逻辑 新增图书
		String sql = "update books set bookName=? , author=? ,price=? , ISBN=? ,  press=? where id = ?";
		Object[] objs = { bookName, author, price, ISBN, press, id };
		int count = DBUtil.update(sql, objs);

		// 3:响应
		response.sendRedirect("ListServlet");
	}
}
update.jsp
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="top.gaojc.bean.Books"%>
<!-- JSP页面引入JSTL标签库,使用指令元素taglib-->
<%@taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()   "://"
			  request.getServerName()   ":"   request.getServerPort()
			  path   "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>修改页面</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<style type="text/css">
.top {
	text-align: center;
	color: red;
	font-size: 40px;
}
tr,td{
    text-align: center;
}
</style>
	</head>

	<body>
	<form action="UpdateBookServlet" method="post">
	  <div class="top">修改页面</div>
		<table border="1" cellspacing="0" cellpadding="1" align="center">
		        <tr>
					<td><span>编号:</span></td>
					<td><input type="text" name="id" value="${books.id}" readonly="readonly"/></td>
				</tr>
		        <tr>
					<td><span>图书名称:</span></td>
					<td><input type="text" name="bookName" value="${books.bookName}" /></td>
				</tr>
				<tr>
					<td><span>作者:</span></td>
					<td><input type="text" name="author" value="${books.author}" /></td>
				</tr>
				<tr>
					<td><span>定价:</span></td>
					<td><input type="text" name="price" value="${books.price}" /></td>
				</tr>
				<tr>
					<td><span>ISBN:</span></td>
					<td><input type="text" name="ISBN" value="${books.ISBN}" /></td>
				</tr>
				<tr>
					<td><span>出版社:</span></td>
					<td><input type="text" name="press" value="${books.press}" /></td>
				</tr>
				<tr>
				    <td colspan="2"><input type="submit" value="更新" align="center"/></td>
				</tr>
		  </table>
		</form>
	</body>
</html>

删除逻辑

DeleteServlet.java代码
代码语言:javascript复制
package top.gaojc.action;

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 top.gaojc.util.DBUtil;

public class DeleteServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1:设置编码(解决post中文乱码的)
		request.setCharacterEncoding("utf-8");

		// 2:获取参数
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);

		// 删除
		String sql = "delete from books where id = ?";
		Object[] objs = { id };
		int count = DBUtil.update(sql, objs);

		// 3:响应
		response.sendRedirect("ListServlet");
	}
}

搜索图书名称获取详细信息

InfoBookNameServlet.java代码
代码语言:javascript复制
package top.gaojc.action;

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 top.gaojc.bean.Books;
import top.gaojc.dao.LoginDao;

public class InfoBookNameServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1:设置编码
		request.setCharacterEncoding("utf-8");

		// 2:根据书名获取参数
		String bookName = request.getParameter("bookName");
		// 根据书名获取详细信息
		Books bookInfo = LoginDao.infoByBookName(bookName);

		// 3:响应
		if (bookInfo != null) {
			request.setAttribute("info", bookInfo);
			request.getRequestDispatcher("bookInfo.jsp").forward(request,
					response);
		} else {
			response.sendRedirect("ListServlet");
		}
	}
}
bookInfo.jsp
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="top.gaojc.bean.Books"%>

<!-- JSP页面引入JSTL标签库,使用指令元素taglib-->
<%@taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme()   "://"
			  request.getServerName()   ":"   request.getServerPort()
			  path   "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>详情页面</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<style type="text/css">
.top {
	text-align: center;
	color: red;
	font-size: 30px;
}

tr,td {
	text-align: center;
}
</style>
	</head>

	<body>
		<form action="ListServlet" method="post">
			<div class="top">详情页面</div>
			<table border="1" cellspacing="0" cellpadding="1" align="center" width="260px">
				<tr>
					<td><span>编号:</span></td>
					<td><span>${info.id}</span></td>
				</tr>
				<tr>
					<td><span>图书名称:</span></td>
					<td><span>${info.bookName}</span></td>
				</tr>
				<tr>
					<td><span>作者:</span></td>
					<td><span>${info.author}</span></td>
				</tr>
				<tr>
					<td><span>定价:</td>
					<td><span>${info.price}</span></td>
				</tr>
				<tr>
					<td><span>ISBN:</td>
					<td><span>${info.ISBN}</span></td>
				</tr>
				<tr>
					<td><span>出版社:</td>
					<td><span>${info.press}</span></td>
				</tr>
				<tr>
					<td colspan="2">
						<a href="ListServlet"><input type="submit" value="返回列表"
								align="center" /> </a>
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

点击图书名称获取详细信息

InfoIdServlet.java代码
代码语言:javascript复制
package top.gaojc.action;

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 top.gaojc.bean.Books;
import top.gaojc.dao.LoginDao;

public class InfoIdServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 1:设置编码
		request.setCharacterEncoding("utf-8");

		// 2:根据id获取参数
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);

		// 根据id获取详细信息
		Books book = LoginDao.updateBookByID(id);

		// 3:响应
		if (book != null) {
			request.setAttribute("info", book);
			request.getRequestDispatcher("bookInfo.jsp").forward(request,
					response);
		} else {
			response.sendRedirect("bookList.jsp");
		}
	}
}
bookInfo.jsp(和上面搜索图书名称的jsp代码一样)
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="top.gaojc.bean.Books"%>

<!-- JSP页面引入JSTL标签库,使用指令元素taglib-->
<%@taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme()   "://"
			  request.getServerName()   ":"   request.getServerPort()
			  path   "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>详情页面</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<style type="text/css">
.top {
	text-align: center;
	color: red;
	font-size: 30px;
}

tr,td {
	text-align: center;
}
</style>
	</head>

	<body>
		<form action="ListServlet" method="post">
			<div class="top">详情页面</div>
			<table border="1" cellspacing="0" cellpadding="1" align="center" width="260px">
				<tr>
					<td><span>编号:</span></td>
					<td><span>${info.id}</span></td>
				</tr>
				<tr>
					<td><span>图书名称:</span></td>
					<td><span>${info.bookName}</span></td>
				</tr>
				<tr>
					<td><span>作者:</span></td>
					<td><span>${info.author}</span></td>
				</tr>
				<tr>
					<td><span>定价:</td>
					<td><span>${info.price}</span></td>
				</tr>
				<tr>
					<td><span>ISBN:</td>
					<td><span>${info.ISBN}</span></td>
				</tr>
				<tr>
					<td><span>出版社:</td>
					<td><span>${info.press}</span></td>
				</tr>
				<tr>
					<td colspan="2">
						<a href="ListServlet"><input type="submit" value="返回列表"
								align="center" /> </a>
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

编写web.xml文件

直接自动写入(直接new一个Servlet文件)

具体演示过程请点击此链接查看

web.xml

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>top.gaojc.action.LoginServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>ListServlet</servlet-name>
    <servlet-class>top.gaojc.action.ListServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>DeleteServlet</servlet-name>
    <servlet-class>top.gaojc.action.DeleteServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>InfoBookNameServlet</servlet-name>
    <servlet-class>top.gaojc.action.InfoBookNameServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>RegServlet</servlet-name>
    <servlet-class>top.gaojc.action.RegServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>UpdateServlet</servlet-name>
    <servlet-class>top.gaojc.action.UpdateServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>UpdateBookServlet</servlet-name>
    <servlet-class>top.gaojc.action.UpdateBookServlet</servlet-class>
  </servlet>
  <servlet>
  <servlet-name>InfoIdServlet</servlet-name>
  <servlet-class>top.gaojc.action.InfoIdServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>UpServlet</servlet-name>
    <servlet-class>top.gaojc.action.UpServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ListServlet</servlet-name>
    <url-pattern>/ListServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>DeleteServlet</servlet-name>
    <url-pattern>/DeleteServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>InfoBookNameServlet</servlet-name>
    <url-pattern>/InfoBookNameServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RegServlet</servlet-name>
    <url-pattern>/RegServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>UpdateServlet</servlet-name>
    <url-pattern>/UpdateServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>UpdateBookServlet</servlet-name>
    <url-pattern>/UpdateBookServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>InfoIdServlet</servlet-name>
    <url-pattern>/InfoIdServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>UpServlet</servlet-name>
    <url-pattern>/UpServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

效果演示

登录页面

列表页面

新增页面

修改页面

详情页面

数据库数据

用户表

图书表

0 人点赞