2016-05-30 48 views
0

喜每个人都可以在任何帮助我...如何将servelet数组列表传递给jsp表?

我想列出从数据库和显示所有的数据把jsp表

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 


    HttpSession session = request.getSession(true); 

    try { 
     Connection con=DataConnection.getconnection(); 
     PreparedStatement pst; 
     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 
     out.println("<html><body>"); 
     pst = con.prepareStatement("select * from [user]"); 
     ResultSet rs = pst.executeQuery(); 
     List<User> ee = new ArrayList<User>(); 

     while (rs.next()) { 
      User e = new User(); 
      e.setName(rs.getString(1)); 
      e.setUserName(rs.getString(2)); 
      e.setPass(rs.getString(3)); 

      ee.add(e); 
     } 

     request.getSession().setAttribute("ee", ee); 
     RequestDispatcher rd = getServletContext().getRequestDispatcher("/user.jsp"); 
     rd.forward(request, response); 

    } catch (Throwable theException) { 
     System.out.println(theException); 
    } 
} 

这是我的代码,它包含数组列表并通过它trought request.getSession()。setAttribute(“ee”,ee);

现在怎么接取在JSP中这个值,它应该在从表 的显示可以在任何给我的示例代码此

+0

尝试在您的jsp页面中: - 列表 ee = new ArrayList (); ee =(列表)request.getAttribute(“ee”); –

+0

这是不可能的gaurav,因为Sameer在会话中不在请求中设置了值。 –

回答

0

使用JSTL

我建议设置数据请求而不是会话

request.setAttribute("ee", ee); 

比使用下面的代码替换线

request.getSession().setAttribute("ee", ee); 

在JSP

<c:forEach items="${ee}" var="element"> 
    <tr> 
    <td>${element.col1}</td> 
    <td>${element.col2}</td> 
    </tr> 
</c:forEach> 
+0

感谢所有对我们的支持...................... –

0

您可以访问使用$ {呀}数组。

有许多使用JSTL进行迭代的方法。

假设你User.java包含例如两个atributes:姓名,您可以访问到这些信息在你的JSP与下面的代码:

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%> 
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

    <c:forEach items="${sessionScope.ee}" var="item"> 
     ${item.name} - ${item.surname} 
    </c:forEach> 

你应该考虑到,你把你的属性帐户在会议中。 (这就是为什么您必须使用$ {sessionScope})确保您是否要使用会话。

+0

这是不可能的,因为Sameer已在Session中设置了值而不是请求 –

+0

yogesh:错误的,看到http://stackoverflow.com/q/5387174 – BalusC

+0

这就是我在我的答案建议。 :)并且在奥斯卡编辑他的答案之前发表了评论。 –