2016-11-03 22 views
0

オンラインバンクを作成しています。ログインすると、そのユーザーのセッションが作成されました。ユーザーは異なるトランザクションタイプをクリックするオプションを持っています。ユーザーがアカウントボタンをクリックすると、openAccount.jspページに移動します。 openAccount.jspには、ユーザーの氏名、姓、電子メールのフォームもあります。ユーザーが開かれているアカウントボタンをクリックしてopenAccount.jspにリダイレクトされると、フォームをデータベースベースからあらかじめ入力します。どのようにわからないのですか?ここでは、これまでに私のコードです:定義済みフォームデータベースのフィールド

家庭page.jsp

<div class="col-md-4"> 
     <form name = "OpenAccount" action="open-account.jsp" id="openaccount"> 
      <p> 
       <button type="submit" class="btn btn-primary btn-lg">Open Account</button> 
      </p> 
     </form> 
    </div> 

オープンaccount.jspが

<!DOCTYPE html> 
<html> 
<head> 
    <title>Open Account</title> 
</head> 
<body> 
    <h3>Please fill in the details</h3> 

    <form name="predifinedFields"> 
     First Name: <input type="text" name="firstname" value= <%= request.getAttribute("firstname") %> > <br/><br/> 
     Last Name: <input type="text" name="lastname" value= <%= request.getAttribute("lastname") %>> <br/><br/> 
     Email: <input type="text" name="email" value= <%= request.getAttribute("email") %>> <br/><br/> 
    </form> 

    <form name="openAccount" action="OpenAccount" method="POST"> 
     Select the type of account: 
     <select name="accounttype"> 
      <option>Checking</option> 
      <option>Saving</option> 
     </select> <br/><br/> 

     Initial Deposit: $<input type="text" name="deposit"> <br/><br/> 

     Please check the box if everything above is complete: 
     Agree <input type="radio" name="agree" value="Agree"> 
     Disagree <input type="radio" name="agree" value="Disagree"> 

     <br/><br/> 
     <input type="submit" value="submit" name="Submit"> 
    </form> 
</body> 
</html> 

OpenAccount.java

public class OpenAccount extends HttpServlet 
{ 
    private static final long serialVersionUID = 1L; 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
    } 


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     String username = ""; 

     HttpSession session = request.getSession(false); 
     if(session != null) 
     { 
      username = (String) session.getAttribute("username"); 

      Users users = new Users(); 
      users = DBConnection.getUsers(username); 

      request.setAttribute("firstname", users.getFirstName()); 
      request.setAttribute("lastname", users.getLastName()); 
      request.setAttribute("email", users.getEmail()); 
      //request.setAttribute("username", users.getUsername()); 
     } 

     // String firstName = (String) request.getParameter("firstname"); 
     // String lastname = (String) request.getParameter("lastname"); 
     String email = (String) request.getParameter("email"); 
     String accountType = (String) request.getParameter("accounttype"); 
     String accept = (String) request.getParameter("agree"); 
     String deposit = (String) request.getParameter("deposit"); 

     if(accept.equals("Agree")) 
     { 
      if(accountType.equals("Checking")) 
      { 
       DBConnection.newCheckingAccount(email, deposit); 
       RequestDispatcher dispatcher = request.getRequestDispatcher("home-page.jsp"); 
       dispatcher.forward(request, response); 
      } 
      else if(accountType.equals("Saving")) 
      { 
       DBConnection.newSavingAccount(email, deposit); 
       RequestDispatcher dispatcher = request.getRequestDispatcher("home-page.jsp"); 
       dispatcher.forward(request, response); 
      } 
     } 
     else 
     { 
      System.out.println("Please modify the changes"); 
      RequestDispatcher dispatcher = request.getRequestDispatcher("open-account.jsp"); 
      dispatcher.forward(request, response); 
     } 

    } 
} 

Database.java

public static Users getUsers(String username) 
    { 
     Users user = new Users(); 
     try 
     { 
      DBConnection.connectToDB(); 
      String query = "SELECT * FROM userlogin where username=?"; 

      stmt = DBConnection.conn.prepareStatement(query); 
      stmt.setString(1,username); 
      ResultSet rs = stmt.executeQuery(); 

      while(rs.next()) 
      { 
       user.setFirstName(rs.getString("firstname")); 
       user.setLastName(rs.getString("lastname")); 
       user.setEmail(rs.getString("email")); 
       user.setUsername(rs.getString("username")); 
       user.setPassword(rs.getString("password")); 
      } 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 

     return user; 
    } 
お使いの場合(基本的なサーブレット)では

答えて

0

、あなたはこのロジックに従ってください:

- >ブラウザ
- あなたが行ったように属性、最終的にopen-account.jsp
に転送負荷や要求に、データベースから必要なデータを準備する>サーブレット - >open-account.jspデータベースから取得したデータでフィールドを動的にレンダリングするリクエスト属性を使用します。他の使用例については

、この単純なロジック維持しよう:
- >ブラウザ
- >サーブレット処理....
- > JSPのレンダリングを....

でフォームhome-page.jspがサーブレットに投稿する必要があります(コントローラ):

<div class="col-md-4"> 
     <form name = "OpenAccount" action="/OpenAccount" id="openaccount"> 
      <p> 
       <button type="submit" class="btn btn-primary btn-lg">Open Account</button> 
      </p> 
     </form> 
    </div> 

あなたはOpenAccountOpenAccountServletに(規則を維持することは、より良いです)名前を変更する必要がありますweb.xml(または注釈付き)にservletOpenAccountServletを正しく設定してください。 あなたはサーブレット構成についてweb.xmlファイル、以前jspaction="/OpenAccount")に投稿されたURLを使用する場合は<url-pattern>/OpenAccount</url-pattern>

の値と一致する必要があり、前方には、そのように実行する必要があります。

getServletContext().getRequestDispatcher("yourRequiredPath/open-account.jsp ").forward(request, response); 
関連する問題