Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 31
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213

    [jsp]svuotare carrello(hashtable,session)

    ho un carrello della spesa creato in questo modo:
    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@ page language="java" import="java.util.*" import="java.lang.*"%>
    <html>  <head><title>'ListaCarrello.jsp'</title>
      <script type="text/javascript">
    	function AllaRicerca() {
    			location.href = ("ricerca.jsp");
    	}
    </script>	
     </head>
     <body>
      <h2 align="center">Il tuo carrello della spesa</h2>
    
    
      <%  Hashtable articolo = (Hashtable)session.getAttribute("Prodotto");
    	if (request.getParameter("idProd") != null && request.getParameter("prod")!= ""){
        	             String strQuant = request.getParameter("Quantità");
         		Integer app = null;
         		int intQuant = app.parseInt(strQuant);
           
         		String strId = request.getParameter("idProd");
         		Integer appoggio = null;
         		int intId = appoggio.parseInt(strId);
         
         		String strNome = request.getParameter("NomeProd");
         		String strDescr = request.getParameter("DescrProd");
         
         		String strPrezzo = request.getParameter("PrezzoProd");
         		Float appog = null;
         		float fltPrezzo = appog.parseFloat(strPrezzo);
        	 
       	 	String[] dati_prod= {strId, strNome, strPrezzo, strQuant};	
      	 	articolo.put(intId, dati_prod);
      	 	session.setAttribute("Prodotto", articolo);
      	 
      	 	articolo=(Hashtable)session.getAttribute("Prodotto");
      	 }  //fine if
      	 String[] valori;
      	 Enumeration lista = articolo.elements();
      %>
        <div align="center">
      	 <table border=0 cellpadding=0 cellspacing=0>
      	<% while (lista.hasMoreElements()){
      	      valori = (String[])lista.nextElement();
      	%>
       	       <tr>     
      	   	<td width="100">
      	             	<%= valori[1] %>
      	 	</td>
      	 	<td width="100">
      	 		Euro <%=valori[2] %>
      	 	</td>
      	 	<td width="100">
      	 		Quantità: <%=valori[3] %>
      	 	</td>
      	      </tr>
      	 <%	 } %>
      	 </table>
     
    
    
    <input type="button" value="Torna in Ricerca" onclick="AllaRicerca()">
    		<input type="button" value="Svuota carrello" name="B3">
    </div>  </body></html>
    al click del pulsante svuota dovrei svuotare il carrello,xò non so dove inserire le istruzioni java e come collegarle col pulsante,la funzione java dovrebbe essere qualkosa di simile:
    codice:
    while (lista.hasMoreElements(){
            Item item=(Item)lista.nextElement();
            articolo.removeElement(item);
    }
    non so neanche se è possibile una cosa del genere,visto che articolo è di tipo Hashtable

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    In java, anche una cosa del genere
    codice:
    articolo = null;
    O meglio, nella classe articolo (se non ce l'hai, creane una che estenda HashTable) potresti aggiungere dei metodi "empty();" e "isEmpty();" che svuotino e controllino se è vuoto il carrello.

    Per quanto riguarda il come: farei un pulsante (o link) - quindi stiamo parlando di (X)HTML alla cui pressione venga eseguito qualcosa del genere

    codice:
    window.location.href += "&action=svuotacarrello";
    Poi nella JSP avrai un request.getParameter("action") e un if che controlla se tale valore sia "svuotacarrello", nel qual caso nell'if eseguirai le opportune azioni (ossia ti liberi del contenuto dell'HashTable nel modo appropriato).
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213
    non c'è un modo x fare tutto in una pagina,senza dover creare un file .java e quindi un import dopo?comunque ora provo seguendo i tuoi consigli...ti ringrazio!!

  4. #4
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213
    anche provando così,col submit i risultati sono nulli
    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@ page language="java" import="java.util.*" import="java.lang.*"%>
    <html>  <head>  	<title>'ListaCarrello.jsp'</title>
      <script type="text/javascript">
    	function AllaRicerca() {
    			location.href = ("ricerca.jsp");
    	}
    </script>	
      </head>
      <body>
      <h2 align="center">Il tuo carrello della spesa</h2>
    
    
      <% 
     Hashtable articolo = (Hashtable)session.getAttribute("Prodotto");
     String strClick = request.getParameter("B3");
     String[] Valori;
     Enumeration Lista = articolo.elements();
     if (strClick != null){
     	while(Lista.hasMoreElements()){
     		Valori = (String[])Lista.nextElement();
     		articolo.remove(Lista);
     	}
     }
      	 	 
    if (request.getParameter("idProd") != null && request.getParameter("prod")!= ""){
    	String strQuant = request.getParameter("Quantità");
    	Integer app = null;
    	int intQuant = app.parseInt(strQuant);
           
    	String strId = request.getParameter("idProd");
    	Integer appoggio = null;
         	int intId = appoggio.parseInt(strId);
         
         	String strNome = request.getParameter("NomeProd");
         	String strDescr = request.getParameter("DescrProd");
         
         	String strPrezzo = request.getParameter("PrezzoProd");
         	Float appog = null;
         	float fltPrezzo = appog.parseFloat(strPrezzo);
        	 
       	String[] dati_prod= {strId, strNome, strPrezzo, strQuant};	
      	articolo.put(intId, dati_prod);
      	session.setAttribute("Prodotto", articolo);
      	 
      	articolo=(Hashtable)session.getAttribute("Prodotto");
      }  //fine if
      	 String[] valori;
      	 Enumeration lista = articolo.elements();
      %>
        <div align="center">
        <form action="ListaCarrello2.jsp" method="post">
      	<table border=0 cellpadding=0 cellspacing=0>
      	<% 
      	while (lista.hasMoreElements()){
      		valori = (String[])lista.nextElement();
       	%>
      	 <tr>
      		<td width="100">
      			<%= valori[1] %>
      		</td>
      		<td width="100">
      			Euro <%=valori[2] %>
      		</td>
      		<td width="100">
      			Quantità: <%=valori[3] %>
      		</td>
      	</tr>
       <%	 } %>
     
    
    
                  <tr>
     	             <td>
    	 	<input type="button" value="Torna in Ricerca" onclick="AllaRicerca()">
    	 	</td>
    	 	<td>
    	 	<input type="submit" value="Svuota carrello" name="B3">
    	 	</td>
    	 </tr>
    </table> </form>	</div>  </body></html>

  5. #5
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213
    facendo la funzione in questo modo:
    <%
    Hashtable articolo = (Hashtable)session.getAttribute("Prodotto");
    Enumeration Lista = articolo.elements();
    String idtmp = request.getParameter("B3");
    if (idtmp != null){
    while(Lista.hasMoreElements()){
    String StrIdProdotto = request.getParameter("idProd");
    articolo.remove(StrIdProdotto);
    session.putValue("Prodotto",articolo);
    }
    }
    mi da questo errore:
    exception

    org.apache.jasper.JasperException: Exception in JSP: /ListaCarrello2.jsp:21

    18: if (idtmp != null){
    19: while(Lista.hasMoreElements()){
    20: String StrIdProdotto = request.getParameter("idProd");
    21: articolo.remove(StrIdProdotto);
    22: session.putValue("Prodotto",articolo);

    root cause

    java.lang.NullPointerException
    forse è sbagliato inserirla all'inizio,ma non vedo altri modi

  6. #6
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    il submit del form lo fai verso una pagina diversa da quella nel titolo... spero sia una discrepanza solo cosmetica (del tag title per intenderci)

    Comuqnue, dovrebbe essere sufficiente
    codice:
    <% 
      Hashtable articolo = (Hashtable)session.getAttribute("Prodotto");
      String strClick = request.getParameter("B3");  
      if (strClick != null){
        articolo = null;
     }
    %>
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  7. #7
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213
    si,il file è salvato come ListaCarrello2.jsp. facendo come dici mi da:
    exception

    org.apache.jasper.JasperException: Exception in JSP: /ListaCarrello2.jsp:43

    40: articolo=(Hashtable)session.getAttribute("Prodotto ");
    41: } //fine if
    42: String[] valori;
    43: Enumeration lista = articolo.elements();
    44: %>
    45: <div align="center">
    46: <form action="ListaCarrello2.jsp" method="post">

    e la stessa root cause.
    ma facendo come dici mi svuota l'hashtable anche in session?

  8. #8
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Tutto il blocco prima di

    if (request.getParameter("idProd") != null ...

    doveva essere sostituito con il pezzo che ho postato.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  9. #9
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213
    sisi è quello che ho fatto ma mi da l'errore che ho postato prima

  10. #10
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Ma da nessuna parte nel mio codice c'è quella riga 43... non passo in rassegna l'Hashtable per niente, semplicemente la metto a null.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.