HTML登錄頁面代碼Java
public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; private UserDao userDao = new UserDaoImpl(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = userDao.getUserByUsernameAndPassword(username, password); if(user == null) { response.sendRedirect("login.jsp"); return; } HttpSession session = request.getSession(); session.setAttribute("user", user); response.sendRedirect("index.jsp"); } }
上述代碼是一個Java Servlet,用來處理HTML登錄頁面的表單提交。在表單提交之前,先獲取頁面傳遞的用戶名和密碼,通過UserDaoImpl來查詢數據庫中是否有匹配的用戶名和密碼。如果匹配不到,就跳轉回登錄頁面;否則,將用戶信息存在session中,同時將頁面跳轉到主頁。