javaweb实现图书管理系统 源代码
时间: 2023-12-14 09:05:06 浏览: 106
以下是一个基于javaweb的图书管理系统的源代码示例:
```java
// 省略了部分代码
// 登录功能
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username"); String password = request.getParameter("password");
User user = new UserDao().login(username, password);
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("login.jsp?error=1");
}
}
}
// 添加图书功能
public class AddBookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookName = request.getParameter("bookName");
String author = request.getParameter("author");
String publisher = request.getParameter("publisher");
String price = request.getParameter("price");
Book book = new Book(bookName, author, publisher, Double.parseDouble(price));
new BookDao().addBook(book);
response.sendRedirect("bookList.jsp");
}
}
// 模糊搜索功能
public class SearchBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String keyword = request.getParameter("keyword");
List<Book> bookList = new BookDao().searchBook(keyword);
request.setAttribute("bookList", bookList);
request.getRequestDispatcher("bookList.jsp").forward(request, response);
}
}
// 借书功能
public class BorrowBookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] bookIds = request.getParameterValues("bookId");
User user = (User) request.getSession().getAttribute("user");
new BorrowDao().borrowBook(user.getUserId(), bookIds);
response.sendRedirect("borrowList.jsp");
}
}
// 还书功能
public class ReturnBookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] borrowIds = request.getParameterValues("borrowId");
new BorrowDao().returnBook(borrowIds);
response.sendRedirect("borrowList.jsp");
}
}
```
阅读全文