Allora vi spiego...ho una pagina: "bibliotecario.jsp" con il seguente codice inerente a questo problema:
codice:
<% String titolo=(String)session.getAttribute("titolo"); String autore=(String)session.getAttribute("autore"); String editore=(String)session.getAttribute("editore"); Integer anno = (Integer)session.getAttribute("anno"); %> <%if(titolo!=null){%> <form action="Servlet" method="get"> <div id="titolo" align="center">
<label><%=titolo%> </label></div> </form> <%} if(autore!=null){%> <form name="autore" action="Servlet" method="get"> <div align="center">
<label><%=autore%> </label></div> </form> <% } if(editore!=null){%> <form name="editore" action="Servlet" method="get"> <div align="center">
<label><%=editore%> </label></div> </form> <% } %>
poi ho un file java: "database.java" con le seguenti funzione che, una mi crea la tabella che verrà inviata alla variabile titolo presente nella jsp di sopra, e l'altra mi serve in "Servlet.java" solo a dare "l'id" del radiobutton corrispondente all'id del libro presente nel database (per sapere con la funzione javascript quale è stato selezionato):
codice:
//N° LIBRI public int nLibri() throws SQLException{ Connection c = DriverManager.getConnection(this.url, this.user, this.pwd); Statement st = c.createStatement(); String sql = "SELECT LIBRI.ID_LIBRO, LIBRI.TITOLO, LIBRI.AUTORE, LIBRI.EDITORE, LIBRI.ANNO " + "FROM LIBRI"; ResultSet rs = st.executeQuery(sql); int numRow=0; while(rs.next()){ numRow++; } rs.close(); st.close(); c.close(); return numRow; } //LA USO IN MODIFICA LIBRO BIBLIOTECARIO public String listaLibri () throws SQLException{ Connection c = DriverManager.getConnection(this.url, this.user, this.pwd); Statement st = c.createStatement(); String sql = "SELECT LIBRI.ID_LIBRO, LIBRI.TITOLO, LIBRI.AUTORE, LIBRI.EDITORE, LIBRI.ANNO " + "FROM LIBRI"; System.out.println("Stringa QUERY -> "+sql); ResultSet rs = st.executeQuery(sql); String strOut=""; int numRow=0; while(rs.next()){ numRow++; } rs = st.executeQuery(sql); if(numRow<1){ strOut="Lista libri vuota!"; } else{ try{ strOut += "<table border=\"1\">"; strOut += "<caption>Libri Presenti nel catalogo:</caption>"; strOut += "<thead>"; strOut += "<tr>"; strOut += "<th>Titolo</th>"; strOut += "<th>Autore</th>"; strOut += "<th>Editore</th>"; strOut += "<th>Anno</th>"; //CHECK PER SELEZIONARE IL LIBRO ************* strOut += "<th>CHECK</th>"; strOut += "</tr>"; strOut += "</thead>"; strOut += "<tfoot>"; strOut += "<tr>"; strOut += "<th></th>"; strOut += "<th>N°: "+numRow+" libri presenti</th>"; strOut += "<th></th>"; strOut += "<th></th>"; strOut += "<th></th>"; strOut += "</tr>"; strOut += "</tfoot>"; strOut += "<tbody>"; int l=1; while(rs.next()){ //System.out.println("Titolo: "+titolo+" = "+ rs.getString(2) + " ha ID: " + rs.getString(1)); strOut += "<tr>"; strOut += "<td>"+rs.getString(2)+"</td>"; strOut += "<td>"+rs.getString(3)+"</td>"; strOut += "<td>"+rs.getString(4)+"</td>"; strOut += "<td>"+rs.getString(5)+"</td>"; //CHECK DI SELEZIONE PER MODIFICA //strOut += "<td><input TYPE=checkbox name=sports VALUE=Football></td>"; strOut+= "<td><input name=\"libro\" id=\""+l+"\" type=\"radio\" value=\"ID\"></td>"; strOut += "</tr>"; l++; } strOut += "</tbody>"; strOut += "</table>"; } catch(SQLException s){ System.out.println(s); } } rs.close(); st.close(); c.close(); return strOut; }
mentre nel file ""servlet.java" ho la gestione dei 2 pulsanti:
codice:
if (operazioni.equals("Modifica Libro")){ try { Database db = new Database(); //ELENCO LIBRI PRESENTI String tabella = db.listaLibri(); // s.setAttribute("titolo",tabella); //Numero Libri presenti nella biblioteca int n = db.nLibri(); //MOSTRO IL PULSANTE PER MODIFICARE tabella += "<form action=\"Servlet\" method=\"get\">"; tabella+= "
"; tabella+= "<div id=\"contenitore\" align=\"center\">"; tabella+= "<input type=\"submit\" name=\"list\" value=\"Modifica\" onClick=\"return radioCheckato("+n+");\">"; tabella+= "<input type=\"submit\" name=\"list\" value=\"Annulla\">"; tabella+= "</div>"; tabella+= "</form>"; tabella+= "<script type=\"text/javascript\">"; tabella+= "nascondoRicerca();"; tabella+= "</script>"; //INVIO IL PULSANTE s.setAttribute("titolo",tabella); //s.setAttribute("autore",pulsante); s.setAttribute("autore",""); s.setAttribute("editore",""); rd = sc.getRequestDispatcher("/bibliotecario.jsp"); } catch (SQLException ex) { System.out.println("DataBase Error: (Modifica Libro) - " + ex.getMessage()); } } String x ="<script>idLibroSelezionato</script>"; //MODIFICA BIBLIOTECARIO if (operazioni.equals("Modifica")){ //PASSO LE LABEL CON I VALORI String label = "<form action=\"Servlet\" method=\"get\">"; label+= "
"; label+= "<div id=\"contenitore\" align=\"center\">"; label+= "
"; //DEVO ASSEGNARE I VALORI ALLE LABEL label+= "<div style=\"width: 150px; float: inherit;\"><label>Titolo:</label><input type=\"text\" name=\"titolo\" value=\"***DEVO METTERE IL VALORE VECCHIO***\"></div>"; label+= "<div style=\"width: 150px; float: inherit;\"><label>Autore:</label> <input type=\"text\" name=\"autore\" value=\"***DEVO METTERE IL VALORE VECCHIO***\"></div>"; label+= "<div style=\"width: 150px; float: inherit;\"><label>Editore:</label> <input type=\"text\" name=\"editore\" value=\"<%=x%>\"></div>"; label+= "<div style=\"width: 150px; float: inherit;\"><label>Anno:</label> <input type=\"year\" name=\"anno\" value=\"***DEVO METTERE IL VALORE VECCHIO***\"</div>"; //label +="<input type=\"hidden\" name=\"Language\" value=\"<script type=\"text/javascript\">return getLibroSelezionato();</script>\">"; label+= "
"; label+= "<input type=\"submit\" name=\"list\" value=\"Apporta Modifica\">"; label+= "<input type=\"submit\" name=\"list\" value=\"Annulla\">"; label+= "</div>"; label+= "</form>"; label+= "<script type=\"text/javascript\">"; label+= "nascondoRicerca();"; label+= "</script>"; //INVIO LE LABEL COL PULSANTE s.setAttribute("titolo",label); s.setAttribute("autore",""); s.setAttribute("editore",""); rd = sc.getRequestDispatcher("/bibliotecario.jsp"); }
Nel codice sopra ho provato creando la variabile "String x" ad assegnargli il return di una funzione javascript ma non ha l'effetto da me desiderato, non è quello che mi interessa, mettere nella text quel valore ma era solo per vedere se riuscivo a passarlo nel file java.
Infine ho in un file javascript: "function.js" il seguente codice (con variabile globale e relativo metodo get e il metodo "radioCheckato" che tramite il n° di libri selezionati nella query sql presente nella funzione java:"nLibri()", verifica che ci sia un radiobutton checkato e mi dice qual'è il suo id che è l'id del libro che mi serve:
codice:
//Variabile Globale per sapere quale libro è stato selezionato tramite radiobutton var idLibroSelezionato; //Controlla che venga selezionato un libro attraverso un radiobutton function radioCheckato(nL){ var bool = false; //ciclo per tutti i libri (nL identifica il nome del radiobutton) while(nL>0){ //identifico i radiobutton con l'id del libro var mod = document.getElementById(nL); if(mod.checked){ idLibroSelezionato=nL; //prendo l'ID del libro selezionato alert(idLibroSelezionato); document.titolo.action = "Servlet"; bool = true; } nL--; } if(bool==false){ alert("SCEGLIERE UN LIBRO: "+ mod); } return bool; } //prendo l'id del libro selezionato function getLibroSelezionato(idLibroSelezionato){ return idLibroSelezionato; }
Spero di esser stato chiaro, ribadisco il mio problema è avere in java l'id che mi restituisce la funzione javascript "radioCheckato" per poter usarlo per estrarre dal database i campi opportuni del libro da visualizzare nelle text html presenti che creo nella "servlet.java" col tasto "Modifica".