今javaの勉強をしていて商品管理のシステムを作っています。
追加ボタンを押すと商品がリストに追加され、それが表示されるという処理をしています。
追加ボタンを押すたびにリストが初期化されてしまう状況です。
何かアドバイスいただけると幸いです。
よろしくお願いします。
サーブレット
package Section3; import java.io.IOException; import java.util.ArrayList; import java.util.List; import DAO.SalesSearchDAO; import bean.Product; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; @WebServlet("/AddSalesServlet") public class AddSalesServlet extends HttpServlet{ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("product_inf"); int quantity = Integer.parseInt(request.getParameter("quantity")); SalesSearchDAO dao = new SalesSearchDAO(); List<Product> saleslist = (dao.search(quantity , code)); HttpSession session = request.getSession(); session.setAttribute("saleslist", saleslist); RequestDispatcher rd = request.getRequestDispatcher("./sales.jsp"); rd.forward(request, response); } }
DAO
public List<Product> search(int quantity , String code){ int product_code = Integer.parseInt(code); List<Product> saleslist = new ArrayList<>(); Connection con = null; Statement smt = null; String sql = null; try { con = getConnection(); smt = con.createStatement(); sql = "SELECT product_name FROM m_product WHERE product_code = " + product_code + " "; rs = smt.executeQuery(sql); while (rs.next()) { String name = rs.getString("product_name"); Product product = new Product(quantity , name , code); saleslist.add(product); } }catch (Exception e) { throw new IllegalStateException(e); }finally { if(smt != null) { try{smt.close();}catch(SQLException ignore){} } if(con != null) { try{con.close();}catch(SQLException ignore){} } } return saleslist; }
JSP
</body> </html><h1>売上登録</h1> <div style="padding: 0 0 20px 46px"> <label style="text-align: center;">売上日</label> <% GregorianCalendar cal = new GregorianCalendar(); SimpleDateFormat format = new SimpleDateFormat("yyyy/M/d"); String datestr = format.format(cal.getTime()); out.println(datestr); %> </div> <div style="text-align: center;"> <form method="POST" action="./AddSalesServlet" style="margin-bottom: 50px"> <div style="boder-bottom: 1px solid"> <label style="padding-rigth: 100px">商品名</label> <select name="product_inf" type = "hidden" style="padding-rigth: 40px; width: 250px;"> <%for (Product product : list) {%> <option value="<%= product.getproduct_code()%>"><%= product.getproduct_name()%></option> <%}%> </select> <label style="padding-rigth: 30px;">数量</label> <input type="text" name="quantity" style="width: 40px;"> <button type="submit"> 追加</button> </div> </form> </div> <% %> <div style="boder: solid 1px"> <table> <tr> <th style="width: 20%">商品名</th> <th style="width: 5%">数量</th> </tr> <% for (Product product : list) { %> <tr> <td><%=product.getproduct_name()%></td> <td><%=product.getquantity()%></td> </tr> <%}%> <% if(saleslist!=null) { for (Product product : saleslist) { %> <tr> <td><%=product.getproduct_name()%></td> <td><%=product.getquantity()%></td> </tr> <% } } %> </table> </div> <form style="margin-bottom: 30px"> <button type="submit" style="margin-left: 83%">登録</button> </form>

回答2件
あなたの回答
tips
プレビュー