Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem pass values from page JSP to another page JSP

  1. #1
    Junior Member
    Join Date
    May 2023
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem pass values from page JSP to another page JSP

    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

    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"));
    } */
    %>
    The variable "quantidade" is the size of student´s in one class. Please help me. For now thank´s a lot. Regards.

  2. #2
    Junior Member
    Join Date
    May 2023
    Location
    USA
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem pass values from page JSP to another page JSP

    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"));
    Last edited by Norm; May 18th, 2023 at 09:40 AM. Reason: Added code tags

  3. #3
    Junior Member
    Join Date
    May 2023
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem pass values from page JSP to another page JSP

    Quote Originally Posted by customlogodesigns View Post
    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&allowPublicKe yRetrieval=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"));
    The error is this page. Can you help me? I don´t no why the List nota came from page-3.jsp null or empty. Thank´s a lot.

  4. #4
    Junior Member
    Join Date
    May 2023
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem pass values from page JSP to another page JSP

    Quote Originally Posted by customlogodesigns View Post
    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>");
    	}
    The error is in this part. Only executes the else, i.e. the list "nota" came from page-3.jsp is null or empty. The problem probably is in this code:
    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);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
    		request.setAttribute("nota", nota);
    Can you help me? Thank you!

  5. #5
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Problem pass values from page JSP to another page JSP

    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.

Similar Threads

  1. Passing a variable from a jsp page to Java code and back to jsp page.
    By Earlcools in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: April 25th, 2024, 05:44 AM
  2. opening a new page and running a script on it, from the old page.
    By Hudsense in forum Other Programming Languages
    Replies: 1
    Last Post: September 25th, 2014, 04:44 AM
  3. Replies: 6
    Last Post: August 4th, 2014, 05:04 AM
  4. In jsp page After decimal i need only 2 values(for ex:477.74)
    By shashib09 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: August 23rd, 2011, 06:00 AM
  5. Get <SELECT> tag values on other jsp page
    By nehakuls in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 18th, 2009, 02:29 AM