Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error passing ArrayList from Servlet to JSP

I have an important question but firs sorry for my english, I only know the basic. Well my problem is that I have an error passing an ArrayList from a servlet to jsp page:

<% ArrayList<Usuario> u= (ArrayList<Usuario>)session.getAttribute("listado");%>
<table align="left" cellpadding="0" cellspacing="1">
    <tr bgcolor="blue">
        <td>Usuario</td><td>Nombre</td>
        <td>Apellido</td><td>Clave</td> 
    </tr>
<% for(int i=0;i<u.size();i++){ %>
<% Usuario usuario = u.get(i); %>
<tr>
<td> <%= usuario.getUsuario() %></td>
<td> <%= usuario.getNombre() %></td>
<td> <%= usuario.getApellido() %></td>
<td> <%= usuario.getClave() %></td>
</tr>
<%} %>


</table>

That's how I'm doing this but I receive an error in:

<% for(int i=0;i<u.size();i++){ %>

What I'm doing wrong? also my servlet Method is like this:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher rd;

    try {
        Connection cn = MySQLConnection.obtenerConexion();
        String sql = "select * from tb_usuario";
        PreparedStatement ps = cn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        ArrayList<Usuario> listado = new ArrayList<Usuario>();
        while (rs.next()){
            Usuario usu = new Usuario(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4));
            listado.add(usu);
        }
        request.setAttribute("listado", listado);
        request.getRequestDispatcher("/listado.jsp");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I hope you could help me!

like image 477
Josue P. Corzo Avatar asked Mar 06 '26 15:03

Josue P. Corzo


1 Answers

You should not use scriptlets in your JSP. You should use EL and tags in your JSP.

e.g.

${listado}
like image 110
Bhesh Gurung Avatar answered Mar 09 '26 03:03

Bhesh Gurung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!