我将参数从 JSP 页面calendar.jsp传递到 servlet connect.java。在 Java 类中,我从数据库传递一个查询。它可以很好地检索数据,但我需要它将结果打印回 JSP 页面。
我尝试了以下代码,但在out.println()
.
@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");
System.out.println("fromDate---->"+fromDate);
System.out.println("toDate---->"+toDate);
String query = "SELECT action_time,user_action,user_ip,user_id FROM ksdi.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;
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");
System.out.println((action_time) + " " + (user_action) + " "+(user_ip) + " "+(user_id) + " ");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("action time"+((ResultSet) request).getString("action_time")+"<br />");
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>
</form>
</body>
</html>