私はJavaを使い慣れていないが、勉強したいと思っている。私は、サーブレットを制御するUsers.javaクラスでMVCモデルを使用しようとしています。私はこれが動作するように近づいているように感じますが、何かがオフになっている必要があります。理想的には、サブミットを押した後、ユーザ名とパスワードがusers.propertiesファイルに対してチェックされ、一致するものがあれば、ユーザは顧客のホームページにリダイレクトされます。一致しない場合、ユーザーは登録ページにリダイレクトされます。登録情報を入力すると、情報はusers.propertiesファイルに保存され、ユーザーはホームページにリダイレクトされます。関連するサーブレットJavaファイルでJavaのログイン検証用のMVCモデル
login.jspを
<form action=Login method="post">
User Name: <input type=text name=userName><br>
Password: <input type=password name=password><br>
<input type=submit value=Login onClick="validate(this.form)"> <br>
</form>
Login.java
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
/* Following two statements are used to obtain the absolute path
of the users.properies file from its relative path. */
ServletContext sc = this.getServletContext();
String propFilePath = sc.getRealPath("/WEB-INF/users.properties");
Users aUser = new Users(userName, password);
if (aUser.validateUser(aUser,propFilePath)) {
response.sendRedirect("CustomerHomePage.jsp");
} else {
response.sendRedirect("Registration.jsp");
}
}
ユーザーがリダイレクトされるのであれば、それはlogin.jspを非常によく似ページに行きます。 register.javaもlogin.javaと非常によく似ています。主な違いは以下の通りです。
register.javaサーブレット
// Registration via the Users object
Users aUser = new Users(userName, password);
// Register the Users object
aUser.registerUser(aUser, propFilePath);
response.sendRedirect("Login.jsp");
最後に、コントローラ。おそらくこれは私が間違って行った場所ですが、私はこれが初めてでどこでも間違っていたと思いました。
public class Users {
private String userName;
private String password;
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;
}
public Users(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
public void registerUser(Users aUser, String propFilePath) {
Properties p = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(propFilePath);
p.load(fis);
p.setProperty(aUser.getUserName(), aUser.getPassword());
p.store(new FileOutputStream(propFilePath), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fis!=null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public boolean validateUser(Users aUser, String propFilePath){
boolean success = true;
Properties p = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(propFilePath);
p.load(fis);
// Check whether the username exists or not
if(!p.containsKey(aUser.getUserName())) {
// Link-redirection
success = false;
} else { // Check whether the password matches or not
String pword = p.getProperty(aUser.getUserName());
if(!pword.equals(aUser.getPassword())) {
success = true; // Link-redirection
} else {
success = false; // Link-redirection
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
success = false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
success = false;
} finally {
if(fis!=null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return success;
}
}
は編集:私はまた、私は、サーブレット
の下部に、このコードを持っていることを追加する必要があり、私は私が
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException { doGet(request, response);
}
soからフォームを追加していなければならないだけでなく、このコードを持っていますログインページが登録ページに送られます。登録情報に入力すると、ログインするために戻るようになります。しかし、再度ログイン情報を入力すると、ホームページには行きません。
Edit2: 私の間違いが見つかりました。私のvalidateUsersメソッドのブール論理を切り替える必要がありました。ルーキーミス。 :(
新しいコード:
else { // Check whether the password matches or not
String pword = p.getProperty(aUser.getUserName());
if(!pword.equals(aUser.getPassword())) {
success = flase; // Link-redirection
} else {
success = true; // Link-redirection
}
}
あなたのフォームは 'method =" post "'ですが、あなたのサーブレットは 'doGet'です - これはあなたの問題ですか?あなたの問題が –
であることを述べてください。問題は、login.jspからcustomerhomeページに行くことができないということです。私はlogin.jspからregistration.jspに行くループで立ち往生しています。私のregister.javaがログイン情報を格納していないか、ログイン認証コードに何か問題があるかどうかはわかりません。 –