2017-02-10 9 views
-1

私は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 
      } 
     } 
+1

あなたのフォームは 'method =" post "'ですが、あなたのサーブレットは 'doGet'です - これはあなたの問題ですか?あなたの問題が –

+0

であることを述べてください。問題は、login.jspからcustomerhomeページに行くことができないということです。私はlogin.jspからregistration.jspに行くループで立ち往生しています。私のregister.javaがログイン情報を格納していないか、ログイン認証コードに何か問題があるかどうかはわかりません。 –

答えて

0

「問題は何でもかまいませんが、あなたがサーバーにデータを投稿しているし、また、あなたがドンとあなたのコードを見て、あなたがポストを使用しているフォームは、これは正しいですtはあなたのコードのdoPost置くあなたのURLにあるが、その後、あなたのサーブレットでのdoGetを使用して、それを処理していることが目に見えるしたいあなたのケースのために

:。

  • のdoGetは、要求されたページ
  • doPostメソッドは、プロセス を行い、結果を返すまたは登録/ホームページ

乾杯にあなたのケーステイクユーザーの場合と同様にすべきである返す必要があります。

+0

申し訳ありませんが、doPostメソッドを追加してください。 doPostのdoGetメソッドを呼び出します。私はすべてをdoPostに入れてみましたが、まだ運がありません。登録情報がusers.propertiesファイルに保存されているかどうか、またはそれをデバッグする方法が不明です。 –

+0

私たちは進歩しており、フォワードに精通しており、リダイレクトを含めて送信することができます。あなたが "login.jspからregistration.jspへ行くループで立ち往生している"と言いました。あなたのコードは上記の通りですが、あなたのロジックが長い場合はリターンを呼び出すことができます。リダイレクトのブロック内で停止/終了を実行する。 「登録情報がusers.propertiesファイルに保存されているかどうか、またはデバッグする方法がわからない」user.propertiesを見るだけでデータが表示されますか?ブロック内にブレークポイントやロガーを追加して、実行が行われたかどうかを確認することができます。あなたの質問をログ情報で更新するだけで、歓声が上がります。 –

+0

Welp。私はアホです。私がしなければならなかったのは、いくつかのブーリアンステートメントを切り替えただけでした。または削除する! validateUsersメソッドの!pword.equalsステートメントから取得します。ご協力ありがとうございました。デバッグのためにコンソールに物を印刷することは多くの助けになりました。 –

関連する問題