gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:35 26 4
gpt4 key购买 nike

下面是servlet和jsp文件的代码

连接.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) {}
}
}
}

日历.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 上的完整结果。谢谢////

最佳答案

创建一个类,例如:

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>

另请参阅:

关于java - 从servlet打印多行查询输出到jsp///,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14957197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com