标签:find request int manage ret 代码 timezone asi gets
数据库的连接(JDBCUTil)
具体代码:
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);
}
}
完整运行截图:
管理员登陆界面
学生登陆界面
登陆失败界面
管理员登陆成功界面
增加图书
删除
学生端
标签:find request int manage ret 代码 timezone asi gets
原文地址:https://www.cnblogs.com/254916-cn/p/14342653.html