JSP with Java bean
<%@ page import="java.util.*" %> <html> <head> <title>WishList</title> <jsp:useBean class="com.java2s.WishList" id="wishList" scope="session"/> <jsp:useBean class="com.java2s.WishListItems" id="wishListItems" scope="application" /> </head> <body> <% Enumeration e = request.getParameterNames(); %> <% Set map = wishList.getMap(); %> <% while(e.hasMoreElements()){ String key = (String)e.nextElement(); if(key.equals(wishListItems.getItem(0).getId())){ map.add(wishListItems.getItem(0)); } if(key.equals(wishListItems.getItem(1).getId())){ map.add(wishListItems.getItem(1)); } if(key.equals(wishListItems.getItem(2).getId())){ map.add(wishListItems.getItem(2)); } if(key.equals(wishListItems.getItem(3).getId())){ map.add(wishListItems.getItem(3)); } if(key.equals(wishListItems.getItem(4).getId())){ map.add(wishListItems.getItem(4)); } } %> Items currently In your Wish List:<br> <% if(map.size()==0){ %> There are no items in your Wish List<br> <% } else { Set set = map; %> <% if (set!=null){ com.java2s.Item[] keys = new com.java2s.Item[0]; keys = (com.java2s.Item[])set.toArray(keys); for (int i=0;i<keys.length;i++){ %> <%=keys[i].getName()%><%="<br>"%> <% } } }%> <br><a href="wishListForm.jsp">Add another Item</a> </body> </html> //wishListForm.jsp <html> <head> <title>WishList</title> <jsp:useBean class="com.java2s.WishList" id="wishList" scope="session"/> <jsp:useBean class="com.java2s.WishListItems" id="wishListItems" scope="application" /> </head> <body> Please Select items for your WishList:<br> <form method="post" action="wishList.jsp"> <input type="checkbox" name="<%=wishListItems.getItem(0).getId()%>"/><%=wishListItems.getItem(0).getName()%> - <%=wishListItems.getItem(0).getName()%></input><br> <input type="checkbox" name="<%=wishListItems.getItem(1).getId()%>"/><%=wishListItems.getItem(1).getName()%> - <%=wishListItems.getItem(1).getName()%></input><br> <input type="checkbox" name="<%=wishListItems.getItem(2).getId()%>"/><%=wishListItems.getItem(2).getName()%> - <%=wishListItems.getItem(2).getName()%></input><br> <input type="checkbox" name="<%=wishListItems.getItem(3).getId()%>"/><%=wishListItems.getItem(3).getName()%> - <%=wishListItems.getItem(3).getName()%></input><br> <input type="checkbox" name="<%=wishListItems.getItem(4).getId()%>"/><%=wishListItems.getItem(4).getName()%> - <%=wishListItems.getItem(4).getName()%></input><br> <input type="submit" value="Add to My WishList"> </form> <p><a href="wishList.jsp">Show me my wishlist</a> <p><a href="logout.jsp">Log Out</a> </body> </html> //logout.jsp <html> <head> <title>WishList</title> <jsp:useBean class="com.java2s.WishList" id="wishList" scope="session"/> </head> <body> <% session.removeAttribute("wishList");%> You have been logged out <p><a href="wishListForm.jsp">Log in again</a> </body> </html> package com.java2s; public class Item { private String id; private String name; public Item(){} public Item(String s, String t){ name=s; id=t; } public String getId(){ return id; } public String getName(){ return name; } } package com.java2s; import javax.servlet.http.HttpSessionBindingListener; import javax.servlet.http.HttpSessionBindingEvent; import java.util.Set; import java.util.HashSet; import java.util.Iterator; public class WishList implements HttpSessionBindingListener { private Set map = new HashSet(); public Set getMap(){ return map; } //Session binding methods public void valueBound(HttpSessionBindingEvent e){ System.out.println("The WishList has been Bound!"); } public void valueUnbound(HttpSessionBindingEvent e){ Item[] keys = new Item[0]; System.out.println("Getting values..."); Iterator it = map.iterator(); while(it.hasNext()){ Item item = (Item)it.next(); System.out.println(item.getName()); } } } package com.java2s; public class WishListItems { private Item[] items = { new Item("ID 1","Name 1"), new Item("ID 2","Name 2"), new Item("ID 3","Name 3"), new Item("ID 4","Name 4"), new Item("ID 5","Name 5") }; public Item getItem(int i){ if (items[i]!=null){ return items[i]; } else { return null; } } }