前提・実現したいこと
ユーザー新規登録機能を作っています。
新規登録画面で、ログインID、パスワード、パスワード(確認用)、名前、誕生日の5つのフォームに情報を入力し、SQLに接続してデータベースに登録する仕様にしています。
その中で、パスワードとパスワード(確認用)のフォームに入力された内容が異なる場合は新規登録画面に戻り、その際、ログインID、名前、誕生日のフォームには入力内容を引き継ぎ、パスワードとパスワード(確認用)のフォームは空欄になるようにしたいのですがうまくいきません。
該当のソースコード
java
1__作成したBeans__ 2package model; 3 4import java.io.Serializable; 5 6public class User implements Serializable { 7 private String id; 8 private String loginid; 9 private String name; 10 private String birthdate; 11 private String password; 12 private String createdate; 13 private String updatedate; 14 15 16 public User(String loginid,String name) { 17 this.loginid = loginid; 18 this.name = name; 19 } 20 21 public User(String id, String loginid, String name, String birthdate, String password, String createdate, 22 String updatedate) { 23 this.id = id; 24 this.loginid = loginid; 25 this.name = name; 26 this.birthdate = birthdate; 27 this.password = password; 28 this.createdate = createdate; 29 this.updatedate = updatedate; 30 } 31 32 public String getId() { 33 return id; 34 } 35 36 public void setId(String id) { 37 this.id = id; 38 } 39 40 public String getLoginid() { 41 return loginid; 42 } 43 44 public void setLoginid(String loginid) { 45 this.loginid = loginid; 46 } 47 48 public String getName() { 49 return name; 50 } 51 52 public void setName(String name) { 53 this.name = name; 54 } 55 56 public String getBirthdate() { 57 return birthdate; 58 } 59 60 public void setBirthdate(String birthdate) { 61 this.birthdate = birthdate; 62 } 63 64 public String getPassword() { 65 return password; 66 } 67 68 public void setPassword(String password) { 69 this.password = password; 70 } 71 72 public String getCreatedate() { 73 return createdate; 74 } 75 76 public void setCreatedate(String createdate) { 77 this.createdate = createdate; 78 } 79 80 public String getUpdatedate() { 81 return updatedate; 82 } 83 84 public void setUpdatedate(String updatedate) { 85 this.updatedate = updatedate; 86 } 87 88 89} 90
java
1__Daoクラスの中のaddInfoメソッド__ 2public void addInfo(String loginid,String password,String password2,String name,String birthdate){ 3 Connection conn=null; 4 try { 5 conn=DBManager.getConnection(); 6 7 String sql="insert into user(login_id,name,birth_date,password,create_date,update_date) values(?,?,?,?,now(),now())"; 8 9 PreparedStatement stmt=conn.prepareStatement(sql); 10 stmt.setString(1, loginid); 11 stmt.setString(2, name); 12 stmt.setString(3, birthdate); 13 stmt.setString(4, password); 14 15 16 int result=stmt.executeUpdate(); 17 18 19 20 21 }catch(SQLException e){ 22 e.printStackTrace(); 23 24 25 }finally { 26 if (conn != null) { 27 try { 28 conn.close(); 29 } catch (SQLException e) { 30 e.printStackTrace(); 31 } 32 } 33 } 34 }
HTML
1__userAdd.jspの入力フォームの部分に関するHTML記述__ 2<label for="user-id" class="control-label col-sm-4">ログインID</label> 3 <div class="col-sm-6"> 4 <input type="text" name="user-id" id="user-id" class="form-control" /> 5 </div> 6 </div> 7 <div class="form-group row"> 8 <label for="password" class="control-label col-sm-4">パスワード</label> 9 <div class="col-sm-6"> 10 <input type="password" name="password" id="password" class="form-control" /> 11 </div> 12 </div> 13 <div class="form-group form-margin row"> 14 <label for="password-confirm" class="control-label col-sm-4">パスワード(確認)</label> 15 <div class="col-sm-6"> 16 <input type="password" name="password-confirm" id="password-confirm" class="form-control"/> 17 </div> 18 </div> 19 <div class="form-group form-margin row"> 20 <label for="user-name" class="control-label col-sm-4">ユーザ名</label> 21 <div class="col-sm-6"> 22 <input type="text" name="user-name" id="user-name" class="form-control" /> 23 </div> 24 </div> 25 <div class="form-group form-margin row"> 26 <label for="continent" class="control-label col-sm-4">生年月日</label> 27 28 <div class="row col-sm-5"> 29 <input type="date" name="birth-day" id="birth-day" class="form-control" size="50" /> 30 </div> 31 </div>
java
1 __UserAddServletの中のdoPostメソッド__ 2 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 4 request.setCharacterEncoding("UTF-8"); 5 6 String loginid=request.getParameter("user-id"); 7 String password=request.getParameter("password"); 8 String password2=request.getParameter("password-confirm"); 9 String name=request.getParameter("user-name"); 10 String birthdate=request.getParameter("birth-day"); 11 12 13 if(!(password.equals(password2))) { 14 15 request.setAttribute("user", loginid); 16 request.setAttribute("user", name); 17 request.setAttribute("user", birthdate); 18 __入力内容をリクエストスコープに保存__ 19 20 21 RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/userAdd.jsp"); 22 dispatcher.forward(request, response); 23 __useAdd.jspにフォワード__ 24 25 26 27 28 29 return; 30 31 32 33 } 34
試したこと
引き継ぎたいフォームの入力内容をリクエストスコープで保存すればいのではと思って試してみたりしましたがうまくいきません。
また、jspファイルのinputタグのなかにvalue属性を作り、そこにEL式を入れるのかなと思ったりもしましたがどうなのでしょうか。
初学者なため質問の仕方等に難があるかもしれません。
その際はすみません。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/03 11:56