To solve your problem of passing values from `page-3.jsp` to `page-4.jsp` and ensuring that the list of notes (`nota`) is not empty or null, we need to ensure that the data is correctly captured and passed between the pages. The primary issue seems to be with how the data is being handled and transferred between the JSP pages. Here's a detailed solution:
Steps to Resolve the Issue
1. Use Form Submission:
Ensure that the form on `page-3.jsp` correctly submits the data to `page-4.jsp`. Instead of relying on hidden fields and request attributes, use standard form submission.
2. Correct Form Handling on `page-3.jsp`:
Create a form that contains all the input fields for the grades and absences, and submit this form to `page-4.jsp`.
3. Process the Submitted Data on `page-4.jsp`:
Retrieve the submitted data in `page-4.jsp` and process it to save into the database.
Example Implementation
`page-3.jsp`
Modify `page-3.jsp` to include a form that posts data to `page-4.jsp`:
```jsp
<%@ page import="java.util.ArrayList, java.util.List" %>
<%
List<Notas> nota = new ArrayList<>();
selectRs.next();
%>
<form method="post" action="page-4.jsp">
<table>
<% 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" name="notaPort<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaHist<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaGeo<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaCien<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaMat<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaEdFis<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaEdArt<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaIng<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaProdT<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="notaExpM<%= selectRs.getInt(1) %>"/></td>
<td><input class="form-control" type="text" name="faltas<%= selectRs.getInt(1) %>"/></td>
</tr>
<% selectRs.next(); } %>
</table>
<input type="hidden" name="bimestre" value="<%= bimestre %>"/>
<input type="hidden" name="quantidade" value="<%= quantidade %>"/>
<button type="submit" class="btn btn-success">Save</button>
</form>
```
`page-4.jsp`
Process the submitted data in `page-4.jsp`:
```jsp
<%@ page import="java.util.ArrayList, java.util.List" %>
<%
String bimestre = request.getParameter("bimestre");
int quantidade = Integer.parseInt(request.getParameter("quantidade" ));
List<Notas> notasListagem = new ArrayList<>();
for (int i = 1; i <= quantidade; i++) {
int studentId = Integer.parseInt(request.getParameter("studentId" + i)); // Ensure you pass student IDs as hidden fields if needed.
Notas notas = new Notas();
notas.setBimestre(bimestre);
notas.setNotaPort(request.getParameter("notaPort" + studentId));
notas.setNotaHist(request.getParameter("notaHist" + studentId));
notas.setNotaGeo(request.getParameter("notaGeo" + studentId));
notas.setNotaCien(request.getParameter("notaCien" + studentId));
notas.setNotaMat(request.getParameter("notaMat" + studentId));
notas.setNotaEdFis(request.getParameter("notaEdFis " + studentId));
notas.setNotaEdArt(request.getParameter("notaEdArt " + studentId));
notas.setNotaIng(request.getParameter("notaIng" + studentId));
notas.setNotaProdT(request.getParameter("notaProdT " + studentId));
notas.setNotaExpM(request.getParameter("notaExpM" + studentId));
String faltasParam = request.getParameter("faltas" + studentId);
int faltasValue = (faltasParam != null && !faltasParam.isEmpty()) ? Integer.parseInt(faltasParam) : 0;
notas.setFaltas(faltasValue);
notas.setFK_IdAluno(studentId);
notasListagem.add(notas);
}
// Check if the list is not null or empty
if (notasListagem != null && !notasListagem.isEmpty()) {
// Proceed with saving the data to the database
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");
String sql = "INSERT INTO notas (conceitos, faltas, bimestre, disciplina, FK_idAluno) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pst = con.prepareStatement(sql);
for (Notas notas : notasListagem) {
pst.setString(1, notas.getNotaPort());
pst.setInt(2, notas.getFaltas());
pst.setString(3, notas.getBimestre());
pst.setInt(4, notas.getFK_IdAluno());
pst.addBatch();
// Repeat for other subjects...
}
pst.executeBatch();
} catch (Exception e) {
e.printStackTrace();
}
} else {
out.println("<h1>ERRO!!!</h1>");
}
%>
```
Summary
1. Ensure proper form submission: Use standard form submission methods to send data from `page-3.jsp` to `page-4.jsp`.
2. Retrieve and process data correctly: In `page-4.jsp`, retrieve the form data using `request.getParameter` methods and store them in a list.
3. Handle database operations: Check if the list is not empty or null, and then proceed with saving the data to the database.
By following these steps, you should be able to correctly pass and handle the data between your JSP pages and ensure that it is saved properly in your database. If you find yourself needing additional
help with programming assignment, there are many resources and services available online, such as those discussed on programming forums and websites like
ProgrammingHomeworkHelp.com, where you can find guidance and support for your coding challenges.