问题描述:

I have one jsp page and one servlet, In servlet i want to fetch some rows from a particular table and pass them to jsp .I am able to retrieve in servlet and trying to pass them in jsp it is not getting displayed please anyone help me out here...

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);

}

}

}

and here is jsp code

<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>

网友答案:

Your for each loop should be like this ,

<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>

And your servlet as ,

     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);

Hope this helps !!

相关阅读:
Top