Reputation: 1
I am trying to create a project where I use a servlet to pass information to a JSP page. The problem is that I can't get the JSP to generate what I expect.
To explain further, in the servlet I retrieve information from a database and store it in an array (in my case, the values are 2 and 3). But when I pass it to the JSP, I can't seem to store that information in another array and then display it on the screen. If I use ${partidas}, it gives me the desired value of 2,3. But when I use `<% List<Integer> partidas = (List<Integer>) request.getAttribute("partidas");` nothing is stored. I have included my code below. Servlet:
import java.io.;
import javax.servlet.;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.;
import java.sql.;
import java.util.*;
public class PartidasAMedias extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Connection con;
Statement st;
ResultSet rs;
PrintWriter out;
String SQL, Nombre;
int IdUsu, Estado;
List<Integer> partidas = new ArrayList<Integer>();
try { Nombre = req.getParameter("nombreRecogido"); System.out.println("Nombre: " + Nombre);
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/cuatroenraya", "root", "");
st = con.createStatement(); SQL = "SELECT IdUsuario FROM usuarios WHERE Nombre ='" + Nombre + "'";
rs = st.executeQuery(SQL);
if (rs.next()) {
System.out.println("2." + rs.getInt("IdUsuario"));
IdUsu = rs.getInt("IdUsuario");
SQL = "SELECT IdPartida, Estado FROM partidas WHERE Creador ='" + IdUsu + "' OR Invitado = '" + IdUsu + "'";
rs = st.executeQuery(SQL);
if (rs.next()) {
Estado = rs.getInt("Estado");
while (rs.next()) {
if (Estado == 1) {
int idPartida = rs.getInt("IdPartida");
System.out.println("3." + idPartida);
partidas.add(rs.getInt("IdPartida"));
System.out.println("Partida agregada: " + rs.getInt("IdPartida"));
}
}
System.out.println("4."+ partidas);
HttpSession session = req.getSession(true);
//session.setAttribute("partidas", partidas);
req.getSession().setAttribute("partidas", partidas);
System.out.println("Redirigiendo a ListadoPartidas.jsp con partidas: " + partidas);
res.sendRedirect("ListadoPartidas.jsp");
} else {
res.sendRedirect("zSinPartidas.html");
}
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
System.err.println(e);
}
}
}
ListadoPartidas.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %> <%@ page import="java.util.List" %>
<!DOCTYPE html>
<html lang="es" dir="ltr">
<head>
<meta http-equiv="Content-Type" contest="text/html; charset=ISO-8859-1">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Partidas en curso</title>
</head>
<body>
${partidas}
<% List<Integer> partidas = (List<Integer>) request.getAttribute("partidas");
if (partidas != null && !partidas.isEmpty()) {
%>
<table>
<thead>
<tr>
<th>ID Partida</th>
</tr>
</thead>
<tbody>
<% for (Integer idPartida : partidas) { %>
<tr>
<td>
<%= idPartida %>
</td>
</tr>
<% } %>
</tbody>
</table>
<% } else { %>
<p>No hay partidas a medias</p>
<% } %>
</body>
</html>
The images I am attaching are what the JSP generates and what I can see in the web server console.Web server console jsp
Upvotes: 0
Views: 40