从servlet打印多行查询输出到jsp ///

时间:2013-02-19 12:11:36

标签: java jsp servlets

以下是servlet和jsp文件的代码

Connect.java

@WebServlet("/calendar")
public class Connect extends HttpServlet {  

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException {

    String fromDate = request.getParameter("fromDate");
    String toDate = request.getParameter("toDate");

    String query = "SELECT action_time,user_action,user_ip,user_id FROM di.login_detail where (action_time, action_time) OVERLAPS (DATE '"
                + fromDate+ "',DATE '"+ toDate+ "'+ integer '1')" +
                        " order by action_time desc";
    Connection conn = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    System.out.println("done4");
    try {

        conn = ConnectionUtil.getConnection();
        statement = conn.prepareStatement(query);           
        resultSet = statement.executeQuery();

           if (resultSet.next()) {                          
            while(resultSet.next()){
                String action_time=resultSet.getString("action_time");
                String user_action=resultSet.getString("user_action");
                String user_ip=resultSet.getString("user_ip");
                String user_id=resultSet.getString("user_id");


                request.setAttribute("action_time",action_time);
                request.setAttribute("user_action",user_action);
                request.setAttribute("user_ip",user_ip);
                request.setAttribute("user_id",user_id);
         RequestDispatcher view = request.getRequestDispatcher("calendar.jsp");  
                        view.forward(request,response);
                       }


        } else {
            System.out.println("not found");
        }
    } catch (SQLException e) {
        throw new ServletException("DB interaction failed", e);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
    }
}
}

Calendar.jsp

<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <script src="<%=path%>/js/calendar.js"></script>
    <link href="<%=path %>/css/calendar.css" rel="stylesheet">
</head>
<body>
<form action="calendar">
            <div class="container">
            From:<input  name="fromDate" type="text"   class="calendarSelectDate" />
            To: <input   name="toDate"   type="text" class="calendarSelectDate" />
            <input type="submit" name="b1" value="Go">
        </div>          
<div id="calendarDiv"></div>

<%= request.getAttribute("action_time")%>
<%= request.getAttribute("user_action")%>
<%= request.getAttribute("user_ip")%>
<%= request.getAttribute("user_id")%>

</form> 

</body>
</html>

我试图从我的servlet向calender.jsp显示结果,上面传递的查询产生多行作为结果,但在jsp页面中它只生成第一行输出并停止执行。还有什么可以在jsp上获取完整的结果。 谢谢////

2 个答案:

答案 0 :(得分:3)

为例如:

创建一个类
public class User {
    private String actionTime;
    private String userAction;
    private String userIp;
    private String userId;

    //Getters and setters
}

然后你应该改变你的代码:

ArrayList<User> users = new ArrayList<User>();
while (resultSet.next()) {
    User user = new User();
    user.setActionTime(resultSet.getString("action_time"));
    user.setUserAction(resultSet.getString("user_action"));
    user.setUserId(resultSet.getString("user_ip"));
    user.setUserIp(resultSet.getString("user_id"));
    users.add(user);
}
request.setAttribute("users", users);
RequestDispatcher view = request.getRequestDispatcher("calendar.jsp");
view.forward(request, response);

在循环中总是创建新用户,将其添加到用户列表中,当循环结束时,将整个列表设置为请求属性。

然后在您的JSP中使用JSTL来处理列表:

<c:forEach var="user" items="${users}">
    <c:out value="${user.actionTime}"/>
    <c:out value="${user.userAction}"/>
    <c:out value="${user.userIp}"/>
    <c:out value="${user.userId}"/>
</c:forEach>

另见:

答案 1 :(得分:0)

如上所述,每次迭代都会覆盖请求属性。你也在打电话

 RequestDispatcher view = request.getRequestDispatcher("calendar.jsp");  
                        view.forward(request,response);

在第一次迭代中转发响应。创建一个列表或哈希并设置该属性,然后从JSP中访问它