管理员,学生以及书籍的实体类
数据库相关DAO接口的定义
Dao接口的实现,与数据库连接
service层接口的定义
service接口的实现
具体代码:
public class JDBCUtil {
private static final String DRIVERNAME = "com.mysql.cj.jdbc.Driver";// jdbc 4.0以后不需要
private static final String URL = "jdbc:mysql://localhost:3306/bookmanage?serverTimezone=Asia/Chongqing";
private static final String USERNAME = "root";
private static final String PASSWORD = "1551963";
public static void registerDriver() {
try {
Class.forName(DRIVERNAME);// jdbc4.0以前需要这句进行驱动注册
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("找不到驱动");
}
}
public static Connection getConnection() throws SQLException {
Connection con = null;
//System.out.println("正在连接数据库...");
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//System.out.println("数据库已连接!");
return con;
}
具体代码:
@WebServlet("/studentLoginServlet")
public class StudentLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession();
StudentService studentService = new StudentServiceImpl();
Student student = studentService.login(username, password);
if (student == null) {
request.setAttribute("login_Error", "用户名或密码错误,请重新输入");
//转发到登录页面
request.getRequestDispatcher("/stuLogin.jsp").forward(request, response);
} else {
//登陆成功 设置Session域参数
session.setAttribute("student", student);
//重定向到success.jsp
response.sendRedirect(request.getContextPath()+"/stuFindBookServlet");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
具体代码:
@WebServlet("/stuFindBookServlet")
public class StuFindBookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentService studentService = new StudentServiceImpl();
List<Book> books = studentService.findBook();
request.setAttribute("books", books);
request.getRequestDispatcher("/student_main.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
具体代码:
@WebServlet("/stuLogoutServlet")
public class StuLogoutServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("student");
request.getRequestDispatcher("/stuLogin.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
输入账号密码。获取HttpSession对象,如果参数为false,那么如果当前会话中存在session则返回,不存在返回null;如果为null,则登入失败,输出用户名或密码错误,请重新输入,并跳回首
页。如果账号密码正确,则进入管理员界面并作出响应。
Session.removeAttribute清空指定属性,删除指定名称的绑定对象;然后返回首页。
调用adminService中查看书籍的功能,并使用request.getRequestDispatcher的请求转发功能将图书信息展示。
调用adminService中的添加图书功能,添加图书,如果添加成功,则response做出响应并跳转到查看书籍界面。
调用adminService中的deleteBook方法,如果删除成功,则response做出响应并跳转到查看书籍界面。
输入图书信息,并调用adminService中的更新功能进行图书修改。如果修改成功,则response做出响应并跳转到查看书籍界面。
使用了Tomcat9.0服务器以及Servlet技术:
封装提供了各种数据访问操作,将具体数据实现和业务逻辑分离。
采用MySQL数据库实现了数据存储
实现使用web网页进行对图书的增删查改功能(对管理员)以及管理员登入程序、退出程序的功能
原文:https://www.cnblogs.com/cxdscx/p/14342708.html