Creare una pagina JSP con alcuni prodotti; si può selezionare solo un prodotto per volta mediante dei radio button; premendo Aggiungi deve essere memorizzata nella sessione una chiave con l’identificativo del prodotto; premendo Totale deve essere visualizzato il totale da pagare.
<%@page import="java.util.Enumeration"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% String[] prodotti = {"<b>PHILIPS - FERRO DA STIRO A VAPORE GC2040/70</b><br/> Potenza 2100 W - Vapore continuo 30 g/min - Colpo di vapore 100 g/min - Vapore verticale", "<b>PHILIPS - FERRO DA STIRO A VAPORE GC4410/22</b><br/> Potenza 2400 W - Vapore continuo fino a 40 g/min - Piastra SteamGlide - Sistema antigoccia", "<b>ROWENTA - FERRO DA STIRO A VAPORE DW8110</b><br/> Potenza 2600 W - Piastra Microsteam inox Laser con 400 micro-fori vapore - Punta alta precisione - Antigoccia", "<b>POLTI - FERRO DA STIRO A CALDAIA VAPORELLA SILVER - EU2014</b><br/> Caldaia con ferro professionale e piastra Activa - Piastra alluminio - Pressione fino a 4 bar - Tasto vapore continuo", "<b>DE'LONGHI - SISTEMA STIRANTE A CALDAIA PRO1840X</b><br/> Potenza 2200 W - Pronto in soli 4 minuti - Caldaia in acciaio inox - AntiCalc System - capacità tanica 1l", "<b>PHILIPS - FERRO A CALDAIA GC7011/20</b><br/> Potenza 2400 W - Piastra scorrevole - Sistema anticalcare - Autonomia illimitata - Serbatoio 1,7 l - Pressione 4,2 bar - Dimensioni compatte", "<b>BOSCH - FERRO DA STIRO A CALDAIA TDS3715100</b><br/> *Valido per acquisti in punto vendita. Operazioni a premi valida dal 19/03 al 05/04/15. Regolamento completo al numero verde 800829120."}; double[] prezzi = {29.99, 49.99, 69.99, 89.00, 139.99, 159.99, 229.99}; %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Negozio on-line</title> </head> <body> <h1>Negozio on-line</h1> <p>Scegli l'articolo che vuoi acquistare e clicca su <b>Aggiungi</b>. Clicca su <b>Totale</b> quando hai finito.</p> <% session = request.getSession(true); String tasto = request.getParameter("tasto"); if (tasto != null) { if (tasto.equals("Aggiungi")) { String nProdotto = request.getParameter("prodotto"); session.setAttribute(nProdotto, new Integer(1)); %> <p><b>Prodotto aggiunto al carrello.</b></p> <% } else if (tasto.equals("Totale")) { double totale = 0.0; Enumeration an = session.getAttributeNames(); while (an.hasMoreElements()) { totale += prezzi[Integer.parseInt((String) an.nextElement())]; } %> <p>Totale da pagare: <b>€ <%=totale%></b></p> <% } // endif tasto = Totale } // endif tasto != null %> <hr/> <form action="index.jsp" method="get"> <% for (int i = 0; i < prodotti.length; i++) { %> <input type="radio" name="prodotto" value="<%=i%>"/><%=prodotti[i]%><br/> <b>Prezzo € <%=prezzi[i]%></b><hr/> <% } // endfor %> <input type="submit" name="tasto" value="Aggiungi"/> <input type="submit" name="tasto" value="Totale"/> </form> </body> </html>