我试图创建一个带有登录页面的个人网站。首先,您必须创建一个帐户,该信息将以文本文件的形式存储在我的计算机中。一切顺利,直到我尝试登录。
为了登录,我写了一个代码来检查用户是否写了他们的帐户信息是对还是错。但每次它总是说User or password is not correct
即使有正确的帐户。到目前为止,这是我的代码
PrintWriter out = response.getWriter();
String user = request.getParameter("u1");
String pass = request.getParameter("u2");
HttpSession session = request.getSession();
try {
FileReader inputFile = new FileReader("D:/data.txt");
BufferedReader bufferReader = new BufferedReader(inputFile);
String a;
String b;
while ((a = bufferReader.readLine()) != null) {
if (a.equals(user)) {
b = bufferReader.readLine();
if (b.equals(pass)) {
session.setAttribute("login", user);
response.sendRedirect("index.jsp");
} else {
session.setAttribute("login failed", "Your username or password is incorrect");
response.sendRedirect("signinfailed.jsp");
break;
}
} else {
session.setAttribute("login failed", "Your username or password is incorrect");
response.sendRedirect("signinfailed.jsp");
break;
}
}
bufferReader.close();
}catch(Exception e){
System.out.println("Exception");
}
finally {
out.close();
}
}