简体   繁体   English

将ArrayList从Servlet传递给JSP时出错

[英]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从servlet传递到jsp页面:

<% 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: 我的servlet方法也是这样的:

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! 我希望你能帮助我!

You should not use scriptlets in your JSP. 您不应该在JSP中使用scriptlet。 You should use EL and tags in your JSP. 您应该在JSP中使用EL和标记。

eg 例如

${listado}

You are setting the variable in request object while retrieving from session , which is not present hence the issue. 您正在从session检索时在request对象中设置变量,这不存在因此问题。

You are setting the attribute in doPost as below": 您正在doPost中设置属性,如下所示:

 request.setAttribute("listado", listado);

You are retrieving the attribute in your JSP as below": 您正在检索JSP中的属性,如下所示:

 <% ArrayList<Usuario> u= (ArrayList<Usuario>)session.getAttribute("listado");%>

Please use the same scope session or request in both places. 请在两个地方使用相同的范围sessionrequest

scriptlets are discouraged to use in ajsp page, use JSTL tags instead. 不鼓励在ajsp页面中使用scriptlets ,而是使用JSTL tags use c-foreach tag to iterate over your arrayList in your jsp page. 使用c-foreach tag迭代jsp页面中的arrayList。 and you are setting an attribute in a request scope and trying to get it in session scope in your jsp. 并且您在请求范围中设置属性并尝试在jsp中的会话范围内获取它。

heres the link which explains c-foreach tag 继承人解释c-foreach标签的链接

you are setting the value in to request scope 您正在设置值以请求范围

  request.setAttribute("listado", listado);

but then trying to access it in session scope. 但后来尝试在会话范围内访问它。

 session.getAttribute("listado");

due to this u might get a null pointer exception in 由于这个原因,你可能会得到一个空指针异常

 u.size()...

try to access it in request scope 尝试在请求范围内访问它

 request.getAttribute("xxxxxx") 

try to avoid adding java code inside JSP whihc is a bad practice. 尽量避免在JSP中添加java代码whihc是一种不好的做法。 use EL and JSTL instead. 请改用EL和JSTL。 you can to the casting part inside the code too.. 你也可以到代码里面的铸造部分..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM