Hi. is my first post. So I´m very begginer Java programer. I have a simple system made in JSP without JSTL or servlets that is used to record student grades and uses a database. Bank connections are OK and users are able to login correctly. After that, the user is sent to a page (let's call it page-2.jsp) where he will select in two HTML select class information and the period of study (called bimester). After that, on the next page (I'll call it page-3.jsp) with this information passed from page-2, an HTML table is set up that has the following structure: the student's id, a number in the class, the name and 10 ( ten) HTML input fields, where the user will enter the students' grades in 9 (nine) fields and one for the students' absences. Now the difficulty. On this page-3.jsp I create a Notes-type list so that the following is saved in this list: the student's id, the nine grades and the absences, so that this is later saved in the database. What help do I need: after the user types the notes and clicks "save" on page-4.jsp (the next one) I can't retrieve this list to be able to insert it into the database. I don't know if it's saving in the list on the previous page and if it's passing an empty or null list. In page-3.jsp I am using a hidden field input feature to retrieve this list in page-4.jsp. I need help checking if the list is going null or empty (it's not being saved in the list) and after that check if it's not empty or null, save it in the database. I don't know if it's possible to save the whole table, because student by student, I can save it. Another important piece of information is that the input fields on page-3.jsp are named like this "<td><input class="form-control" type="text" id="notaPort" name="notaPort<%= selectRs.getInt(1) %>"/></td>" then each grade has the name of the grade plus the student id, for example "notePort234". Here are the codes:
page-2.jsp
<form method="post" action="abrirClasse3.jsp"><!-- era abrirClasse e depois abrirClasse2.jsp. Cheguei no 3....jsp --> <select class="form-select form-select-sm" name="classe"> <option value="" enabled>Selecione a classe</option> <option value="1º ANO A" ><% %>1º ANO A</option> <option value="1º ANO B" >1º ANO B</option> <option value="2º ANO A" >2º ANO A</option> <option value="2º ANO B" >2º ANO B</option> <option value="3º ANO A" >3º ANO A</option> <option value="3º ANO B" >3º ANO B</option> <option value="3º ANO C" >3º ANO C</option> <option value="4º ANO A" >4º ANO A</option> <option value="4º ANO B" >4º ANO B</option> <option value="5º ANO A" >5º ANO A</option> <option value="5º ANO B" >5º ANO B</option> <option value="9º ANO A" >9º ANO A</option> </select> <br><br> <select class="form-select form-select-sm" name="bimestre"> <option value="" enabled>Selecione o Bimestre</option> <option value="1º BIMESTRE">1º BIMESTRE</option> <option value="2º BIMESTRE">2º BIMESTRE</option> <option value="3º BIMESTRE">3º BIMESTRE</option> <option value="4º BIMESTRE">4º BIMESTRE</option> <option value="5º CONCEITO">5º CONCEITO</option> </select> <br><br> <div class="text-center"> <input class="btn btn-success w-100" name ="listar" id="listar" type="submit" value="Listar Alunos"> </div> </form> <br> </div> </div> <div class="col-xs-4 col-sm-4 col-lg-4 p-1 mb-2 bg-white border border-1 border-secondary rounded float-start"> <a class="justify-content-center text-center" href='index.jsp'>Sair</a> <br><br><br><br><br><br> </div> <script src="js/bootstrap.min.js"></script> </body> </html>
page-3.jsp
<% try{ //if(selectRs.next() == false){ //response.sendRedirect("erro2.html"); //}//fim if //else { //List<Aluno> alunos = new ArrayList<>(); List<Notas> nota = new ArrayList<>(); selectRs.next(); for(int i = 0; i < quantidade; i++){ %> <tr> <td><%= selectRs.getInt(1) %></td> <td><%= i1+=1 %></td> <td><%= selectRs.getString(2) %></td> <td><input class="form-control" type="text" id="notaPort" name="notaPort<%= selectRs.getInt(1) %>"/></td> <td><input class="form-control" type="text" id="notaHist" name="notaHist<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="notaGeo" name="notaGeo<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="notaCien" name="notaCien<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="notaMat" name="notaMat<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="notaEdFis" name="notaEdFis<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="notaEdArt" name="notaEdArt<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="notaIng" name="notaIng<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="notaProdT" name="notaProdT<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="notaExpM" name="notaExpM<%= selectRs.getInt(1) %>" /></td> <td><input class="form-control" type="text" id="faltas" name="faltas<%= selectRs.getInt(1) %>" /></td> </tr> <% //Aluno aluno = new Aluno(); Notas notas = new Notas(); notas.setBimestre(bimestre); notas.setNotaPort(request.getParameter("notaPort" + selectRs.getInt(1))); notas.setNotaHist(request.getParameter("notaHist" + selectRs.getInt(1))); notas.setNotaGeo(request.getParameter("notaGeo" + selectRs.getInt(1))); notas.setNotaCien(request.getParameter("notaCien" + selectRs.getInt(1))); notas.setNotaMat(request.getParameter("notaMat" + selectRs.getInt(1))); notas.setNotaEdFis(request.getParameter("notaEdFis" + selectRs.getInt(1))); notas.setNotaEdArt(request.getParameter("notaEdArt" + selectRs.getInt(1))); notas.setNotaIng(request.getParameter("notaIng" + selectRs.getInt(1))); notas.setNotaProdT(request.getParameter("notaProdT" + selectRs.getInt(1))); notas.setNotaExpM(request.getParameter("notaExpM" + selectRs.getInt(1))); String faltasParameter = request.getParameter("faltas" + selectRs.getInt(1)); int faltasValue = 0; if(faltasParameter != null && !faltasParameter.isEmpty()){ faltasValue = Integer.parseInt(faltasParameter); } notas.setFaltas(faltasValue); notas.setFK_IdAluno(selectRs.getInt(1)); nota.add(notas); selectRs.next(); }//fim for //String quantidade2 = String.valueOf(quantidade); request.setAttribute("nota", nota); //request.setAttribute("quantidade", quantidade2); out.println("<input type='hidden' id='bimestre' name='bimestre' value='"+bimestre+"'/>"); out.println("<input type='hidden' id='quantidade' name='quantidade' value='"+quantidade+"'/>"); out.println("<input type='hidden' id='nota' name='nota' value='"+nota+"'/>"); //}//fim else }//fim try catch(Exception e){ e.getStackTrace(); }//fim catch*/ //request do nota aqui %> </table>
page-4.jsp
The variable "quantidade" is the size of student´s in one class. Please help me. For now thank´s a lot. Regards.String bimestre = request.getParameter("bimestre"); session.setAttribute("bimestre", bimestre); out.println("<br>"); out.println("<p><h3>Bimestre: " + bimestre + "</h2></p>"); String quantidade = request.getParameter("quantidade"); session.setAttribute("quantidade", quantidade); out.println("<br>"); out.println("<p><h3>Quantidade de alunos na classe: " + quantidade + "</h2></p>"); String[] notas2 = request.getParameterValues("nota"); if(notas2 != null){ out.println("<br>"); out.println("<p><h3>IdNotas: " + notas2 + "</h2></p>"); for(int i = 0; i < notas2.length; i++){ out.println("<p><h3>IdNotas: " + notas2[i] + "</h2></p>"); } }else { out.println("<p><h1>erro!</h1></p>"); } List<Notas> notasListagem = (List<Notas>) request.getAttribute("nota"); session.setAttribute("nota", notasListagem); if(notasListagem != null && !notasListagem.isEmpty()){ for(int z = 0; z < notasListagem.size(); z++){ notasListagem.contains(z); //out.println("<p><h3>IdNotas: " + notas + "</h2></p>"); } }else { out.println("<h1>ERRO!!!</h1>"); } //imprimir array para ver se esta nulo /*for(int i = 0; i < idAluno.length; i++){ out.println("Id de Aluno: " + idAluno[i]); } try{ Connection con = null; Class.forName("com.mysql.cj.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projetoescola?useTimezone=true&serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false", "root", "root");//?useTimezone=true&serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false String sql = "INSERT INTO 'notas' ('conceitos', 'faltas', 'bimestre', 'disciplina', 'FK_idAluno') VALUES (?, ?, ?, ?, ?);"; PreparedStatement pst = con.prepareStatement(sql); for(int i = 0; i < quantidade2; i++){ pst.setString(1, notaPort[i]); pst.setString(4, port); pst.setString(1, notaHist[i]); pst.setString(4, hist); pst.setString(1, notaGeo[i]); pst.setString(4, geo); pst.setString(1, notaCien[i]); pst.setString(4, cien); pst.setString(1, notaMat[i]); pst.setString(4, mat); pst.setString(1, notaEdFis[i]); pst.setString(4, edfis); pst.setString(1, notaIng[i]); pst.setString(4, ing); pst.setString(1, notaEdArt[i]); pst.setString(4, edart); pst.setString(1, notaProdT[i]); pst.setString(4, prodT); pst.setString(1, notaExpM[i]); pst.setString(4, expM); pst.setString(2, faltas[i]); pst.setString(3, bimestre); pst.setString(5, idAluno[i]); pst.addBatch(); } pst.executeBatch(); } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); String erro = sw.toString(); //Aqui obtenho a String out.println("Erro ao cadastrar: "); out.println("<br><br>"); out.println(erro); //e.printStackTrace(); //response.sendRedirect( "erro.html?excecao=" + URLEncoder. encode(e, "ISO-8859-1")); } */ %>