示例代码:
1. Users类
package com.po; public class Users { private String username; private String password; public Users(){} public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2. DAO类
package com.dao; import com.po.Users; public class UsersDAO { // 用户登录方法 public boolean usersLogin(Users u){ return ("admin".equals(u.getUsername()) && "admin".equals(u.getPassword())); } }
3. HTML代码
<%@ page contentType="text/html;charset=UTF-8" %> <jsp:useBean id="loginUser" class="com.po.Users" /> <jsp:useBean id="userDAO" class="com.dao.UsersDAO" /> <jsp:setProperty name="loginUser" property="*" /> <% // 如果用户名和密码都等于admin,则登录成功 if(userDAO.usersLogin(loginUser)){ session.setAttribute("loginUser", loginUser.getUsername()); request.getRequestDispatcher("login_success.jsp").forward(request, response); }else { response.sendRedirect("login_failure.jsp"); } %>