<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.sql.*"%>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
Verify User
</title>
</head>
<body>
<%
String userid = request.getParameter("userID");
String password = request.getParameter("pwd");
Connection conn = null;
try {
//Step1: JDBC Driver
Class.forName("com.mysql.jdbc.Driver");
//Step2: Define Connection URL
String connURL = "jdbc:mysql://localhost/assignment?user=root&password=root";
//Step3: Establish connection to URL
conn = DriverManager.getConnection(connURL);
String sql = "select * from login where UserId=? and Password=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,userid);
pstmt.setString(2,password);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
session.invalidate();
session = request.getSession();
session.setAttribute("LOGIN-STATUS", "YES");
session.setAttribute("name",userid);
response.sendRedirect("index.jsp");
}
else{
request.setAttribute("errorMessage", "Invalid user or password");
RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
rd.forward(request, response);
}
}catch (Exception e){
System.out.println(e.getMessage());
}finally{
conn.close();
}
%>
</body>
</html>
我打算做的是创建一个登录页面。因此,当我使用正确的用户名和密码成功登录时,它会将我带到另一个页面。但是我现在面临一个问题,我使用正确的用户名和密码登录,发生此错误。我该如何解决这个问题?