jspの
<a herf="cart?action=clear"><button>クリア</button></a>
でdoGETでcart(sessionで作ったhashmap)の中身を消し、同じページに戻りたいのですが、クリアを押すと404エラーが出ます。
単に、session.removeAttribute("clear");
だけではいけないのはわかるのですが、何をどう付け加えればいいのかわかりません。
御教授お願いします。
修正します。
同一のjspのformからキーと値を入力して、POSTで送ってhashmapのcartに登録して、現在のカート内容のdiv内に表示しています。クリアボタンを押すcartの中身が削除されて、下記のtable内の値の表示が消えるようにしたいです。
関係性ですが、サーブレットでの処理はすべて同じサーブレット内で行い。フォームからの入力、表示、クリアボタンもshow.jsp内にあります。
show.jpのフォームからPOSTでdoPOST内でsessionでhashmap内に名前と個数を登録します。
登録した情報をカートの内容の部分に追加していきます。クリアのボタンを押すとcart内の値をすべて削除し、show.jspのカート内容の部分が消えるようにしたいです。処理はこのCart.javaとshow.jspだけで行っています。
Cart.java
import
1import java.util.HashMap; 2 3import javax.servlet.ServletException; 4import javax.servlet.annotation.WebServlet; 5import javax.servlet.http.HttpServlet; 6import javax.servlet.http.HttpServletRequest; 7import javax.servlet.http.HttpServletResponse; 8import javax.servlet.http.HttpSession; 9 10/** 11 * Servlet implementation class Cart 12 */ 13@WebServlet("/Cart") 14public class Cart extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 /** 18 * @see HttpServlet#HttpServlet() 19 */ 20 public Cart() { 21 super(); 22 // TODO Auto-generated constructor stub 23 } 24 25 /** 26 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 27 */ 28 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 29 getServletContext().getRequestDispatcher("/WEB-INF/show.jsp").forward(request,response); 30 HttpSession session = request.getSession(); 31 HashMap<String,Integer> cart = (HashMap<String,Integer>)session.getAttribute("cart");//保持されてる値を持ってきている 32 if(cart == null){//マップが作られていなかったらマップを作る 33 cart = new HashMap(); 34 }; 35 session.removeAttribute("cart"); 36 //getServletContext().getRequestDispatcher("/WEB-INF/show.jsp").forward(request,response); 37 } 38 39 /** 40 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 41 */ 42 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 43 request.setCharacterEncoding("utf-8"); 44 String name = request.getParameter("name"); 45 String countString = request.getParameter("count"); 46 Integer count = Integer.valueOf(countString); 47 HttpSession session = request.getSession(); 48 HashMap<String,Integer> cart = (HashMap<String,Integer>)session.getAttribute("cart");//保持されてる値を持ってきている 49 if(cart == null){//マップが作られていなかったらマップを作る 50 cart = new HashMap(); 51 }; 52 //名前(key)が登録されていなかったら、マップにそのまま名前と個数を登録 53 //名前が登録していたら、値をプラスする 54 if(!(cart.containsKey(name))){//同じ名前のKeyがなければ実行 55 //get(name)はname(key)に対応する値(count)を求める 56 System.out.println("名前:"+name+" 個数:"+count); 57 cart.put(name,count);//名前と個数を登録 58 }else{//同じ名前のKeyがあった時に実行 59 System.out.println("名前:"+name+" 入力個数:"+count+" 合計:"+(count+cart.get(name))); 60 cart.put(name,count+cart.get(name)); 61 }; 62 System.out.println("cart.keySet():"+cart.keySet()); 63 System.out.println("cart.get(name"+cart.get(count)); 64 for (String str : cart.keySet()) { 65 System.out.println(str + ":" + cart.get(str)); 66 } 67 session.setAttribute("cart", cart); 68 getServletContext().getRequestDispatcher("/WEB-INF/show.jsp").forward(request,response); 69 } 70 71}
show.jsp
pageEncoding="UTF-8"%> <%@ page import="java.util.HashMap" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>ショッピングカート</title> </head> <body> <div style="border:#000000 solid 1px;"> <form action="Cart" method="POST"> <table> <tr><td>名前:</td><td><input type="text" name="name" value="ごはん"></td><td></td></tr> <tr><td>個数:</td><td><input type="text" name="count" value="1"></td><td></td></tr> <tr><td></td><td></td><td><button>更新</button></td></tr> </table> </form> </div> <br> <div style="border:#000000 solid 1px;"> <table> <% HashMap<String,Integer> cart = (HashMap<String,Integer>)session.getAttribute("cart"); %> <thead><tr><td>現在のカートの内容</td></tr></thead> <tbody> <% if(cart != null){ for(String name : cart.keySet()){ %> <tr><td bgcolor="#00bfff"><%= name %></td><td><%= cart.get(name) %></td><td></td></tr> <% } };%> </tbody> </table> <a href="cart?action=cart"> <button>カートをクリア</button></a> </div> </body> </html>
回答2件
あなたの回答
tips
プレビュー