打印从servlet传递的表中的完整行

时间:2014-04-30 07:33:53

标签: java jsp servlets

我有一个jsp页面和一个servlet,在servlet中我想从特定的表中获取一些行并将它们传递给jsp。我能够在servlet中检索并尝试在jsp中传递它们但是它没有显示出来有人帮帮我...

public class TimeDetail extends HttpServlet {

    @SuppressWarnings("unchecked")
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        HttpSession session = request.getSession(true);
        String eid = (String) session.getAttribute("eid");
        int count = 0;
        Connection con = ConnectionManager.getConnection();
        try {
            Statement st = con.createStatement();
            Statement st1 = con.createStatement();
            String Query = "select date, intime, outtime, eid  from fulltime where eid='" + eid + "'";
            ArrayList Rows = new ArrayList();
            ResultSet rs = st.executeQuery(Query);
            List agentList = new ArrayList();

            while (rs.next()) {
                ArrayList row = new ArrayList();
                for (int i = 1; i <= 4; i++) {
                    row.add(rs.getString(i));
                }

                String n1 = rs.getString("date");
                String n2 = rs.getString("intime");
                String n3 = rs.getString("outtime");
                String n4 = rs.getString("eid");

                session.setAttribute("n1", n1);
                session.setAttribute("n2", n2);
                session.setAttribute("n3", n3);
                session.setAttribute("n4", n4);

                // response.sendRedirect("TimeDetail.jsp");
                Rows.add(row);

            }
            request.getSession().setAttribute("results", Rows);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/TimeDetail.jsp");
            rd.forward(request, response);

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

这里是jsp代码

<tr><td>
    <table width="600" height="300"align=center cellspacing=0 border="0" 
        <caption><h2>List of users</h2></caption>
        <tr>
            <th>date</th>
            <th>intime</th>
            <th>outtime</th>
            <th>eid</th>
        </tr>
        <c:forEach var="user" items="${Rows.rows}">
            <tr>
                <td><c:out value="${user.date}" /></td>
                <td><c:out value="${user.intime}" /></td>
                <td><c:out value="${user.outtime}" /></td>
                <td><c:out value="${user.eid}" /></td>
            </tr>
      </c:forEach>
    </table>
   </div>
        </table><BR>
        </fieldset>
    </td></tr></table>
   </td></tr>
    </table>
   </body>
   </html>

1 个答案:

答案 0 :(得分:1)

你的每个循环应该是这样的,

<c:forEach var="user" items="${results}">
            <tr>
                <td><c:out value="${user}" /></td>
                <td><c:out value="${user}" /></td>
                <td><c:out value="${user}" /></td>
                <td><c:out value="${user}" /></td>
            </tr>
      </c:forEach>

你的servlet是,

     try {
            Statement st = con.createStatement();
            Statement st1 = con.createStatement();
            String Query = "select date, intime, outtime, eid  from fulltime where eid='" + eid + "'";
            ArrayList row = new ArrayList();
            ResultSet rs = st.executeQuery(Query);


     while (rs.next()) { 
                     row.add(rs.getString("date"));
                     row.add(rs.getString("intime"));
                     row.add(rs.getString("outtime"));
                     row.add(rs.getString("eid"));
    }
}
request.getSession().setAttribute("results", row);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/TimeDetail.jsp");
            rd.forward(request, response);

希望这会有所帮助!!