JAVA POST送信複数 初期表示
JAVAで新規会員登録機能を作ってます。
情報入力画面と修正画面を別々にクラスを分けていたのですが、情報入力画面を一括りしたいと思ってます。
一括りにする際にフィールド値をセットするクラスで複数POSTの使い方がわからないこと、初期表示をがわかりません。
FixInsert系のファイルをInsertに一まとめにしたいです。FixInsert系のファイルは削除したいです。
入力画面(insert.jsp)
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>マイページ</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="main"> <h1>入力画面</h1> <form action="insert" method="post"> <label for="email">Email</label> <input type="text" id="email=" name="email" value ="${customer.email}"> <label for="password">Passowrd</label> <input type="text" id="password" name="password" value ="${customer.password}"> <label for="name">氏名</label> <input type="text" id="name=" name="name" value="${customer.name}"> <label for="gender">性別</label> <input type="radio" name="gender" value="0">男 <input type="radio" name="gender" value="1">女 <label for="blood">血液型</label> <select name="blood"> <option value="${customer.blood}">${customer.blood}型</option> <option value="A">A型</option> <option value="B">B型</option> <option value="O">O型</option> <option value="AB">AB型</option> </select> <label for="old">年齢</label> <input type="text" id="old" name="old" value ="${customer.old}"> <label for="memo">メモ</label> <textarea name="memo" rows="4" cols="41" maxlength="100">${customer.memo}</textarea> <input type="submit" value="確認"> </form> </div> </body> </html>
入力画面の値をフィールド値セット(InsertController.java)
package controller; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import data.Customer; @WebServlet("/insert") public class InsertController extends HttpServlet { protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ request.setCharacterEncoding("UTF-8"); String email = request.getParameter("email"); String password = request.getParameter("password"); String name = request.getParameter("name"); String blood = request.getParameter("blood"); String gender = request.getParameter("gender"); int old =Integer.parseInt(request.getParameter("old")); String memo= request.getParameter("memo"); Customer customer = new Customer(); customer.setEmail(email); customer.setPassword(password); customer.setName(name); customer.setGender(gender); customer.setBlood(blood); customer.setOld(old); customer.setMemo(memo); request.setAttribute("customer", customer); ServletContext sc = getServletContext(); sc.getRequestDispatcher("/" + "confirm.jsp").forward(request, response); } }
会員登録か修正、選択画面(confirm.jsp)
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>マイページ</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <h1>確認画面</h1> <form action="confirm" method="post"> <label for="email">Email</label> <input type="text" id="email=" name="email" value="${customer.email}" disabled> <label for="password">Password</label> <input type="text" id="password" name="password" value="${customer.password}" disabled> <label for="name">氏名</label> <input type="text" id="name" name="name" value="${customer.name}" disabled> <label for="gender">性別</label> <c:if test="${customer.gender == 0}"> <input type="radio" name="gender" value="0" checked=checked>男 <input type="radio" name="gender" value="1">女 </c:if> <c:if test="${customer.gender == 1}"> <input type="radio" name="gender" value="0">男 <input type="radio" name="gender" value="1" checked=checked>女 </c:if> <input type="hidden" id="blood=" name="blood"value="${customer.blood}" disabled> <input type="text" id="old" name="old" value="${customer.old}" disabled> <input type="text" id="memo" name="memo" value="${customer.memo}" disabled> <input type="submit" value="登録"> </form> <form action="fixinsert" method="post"> <input type="hidden" id="email=" name="email" value="${customer.email}"> <input type="hidden" id="password" name="password" value="${customer.password}"> <input type="hidden" id="name" name="name" value="${customer.name}"> <input type="hidden" id="gender" name="gender" value="${customer.gender}"> <input type="hidden" id="blood=" name="blood"value="${customer.blood}"> <input type="hidden" id="old" name="old" value="${customer.old}"> <input type="hidden" id="memo" name="memo" value="${customer.memo}"> <input type="submit" value="修正"> </form> </body> </html>
修正画面、初期表示(fixinsert.jsp)
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>マイページ</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="main"> <h1>入力画面</h1> <form action="confirm" method="post"> <c:forEach var="customer" items="${customerList}"> <label for="email">Email</label> <input type="text" id="email=" name="email" value ="${customer.email}"> <label for="password">Passowrd</label> <input type="text" id="password" name="password" value ="${customer.password}"> <label for="name">氏名</label> <input type="text" id="name=" name="name" value="${customer.name}"> <label for="gender">性別</label> <c:if test="${customer.gender == 0}"> <input type="radio" name="gender" value="0" checked=checked>男 <input type="radio" name="gender" value="1">女 </c:if> <c:if test="${customer.gender == 1}"> <input type="radio" name="gender" value="0">男 <input type="radio" name="gender" value="1" checked=checked>女 </c:if> <label for="blood">血液型</label> <select name="blood"> <option value="${customer.blood}">${customer.blood}型</option> <option value="A">A型</option> <option value="B">B型</option> <option value="O">O型</option> <option value="AB">AB型</option> </select> <label for="old">年齢</label> <input type="text" id="old=" name="old" value ="${customer.old}"> <label for="memo">メモ</label> <textarea name="memo" rows="4" cols="41" maxlength="100">${customer.memo}</textarea> <input type="hidden" name="id" value="${customer.id}"> <input type="submit" value="更新"> </c:forEach> </form> </div> </body> </html>
fixinsert.jspに初期表示 フィールド値セット(FixInsertController.java)
package controller; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import data.Customer; @WebServlet("/fixinsert") public class FixInsertController extends HttpServlet{ protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ request.setCharacterEncoding("UTF-8"); String email = request.getParameter("email"); String password = request.getParameter("password"); String name = request.getParameter("name"); String blood = request.getParameter("blood"); String gender = request.getParameter("gender"); int old =Integer.parseInt(request.getParameter("old")); String memo= request.getParameter("memo"); List<Customer>customerList = new ArrayList<Customer>(); Customer customer = new Customer(); customer.setEmail(email); customer.setPassword(password); customer.setName(name); customer.setGender(gender); customer.setBlood(blood); customer.setOld(old); customer.setMemo(memo); customerList.add(customer); request.setAttribute("customerList", customerList); ServletContext sc = getServletContext(); sc.getRequestDispatcher("/" + "fixinsert.jsp").forward(request, response); } }
使用しているもの
Eclipse2020-12
Tomcat
あなたの回答
tips
プレビュー