4

我将参数从 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>
4

3 回答 3

3

1)在您的 servlet 代码中,您做错了。您应该使用PrintWriter 的输出println() 使用 RequestDispatcher 的forward()方法。但不是都在同一个 servlet 的方法中。

从这里引用:什么是请求调度程序,我如何使用它?

与“包含”的情况不同,“转发”丢弃了 Servlet 写入响应的先前输出

请参阅上述页面中的示例。

如果您选择使用forward()then 传递先前从数据库中检索的数据,您可以将该数据存储在请求范围内,即设置请求属性,例如

request.setAttribute("actionTime", action_time);

当然,你必须打电话之前做forward()

然后在您的 JSP 中,您可以使用Expression Languge输出该数据,如下所示:
test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <h1>Testing the passed values</h1>
    <p>Action time: ${actionTime}</p>
    <!-- same way for other data -->
</body>
</html>


2)另一个问题,在您的servlet代码中有一行:
((ResultSet) request).getString("action_time")

ServletRequest和HttpServletRequest没有方法。尽管ResultSet有这样的方法,但是在你的情况下,将请求转换为ResultSet是没有意义和错误的。getString()


PS
不鼓励在 JSP 中使用 scriplets

于 2013-02-17T00:32:41.710 回答
0

可以将返回的HttpServletRequest请求强制转换为 a ResultSet,我认为它应该使用不强制转换的ResultSet'getString方法。

   PrintWriter out = response.getWriter();  
   out.println("action time"+ resultSet.getString("action_time") + "<br />");  
于 2013-02-16T10:31:15.110 回答
0

在 servlet 中像这样设置您获取到 httprequest 的

PS:我已经评论了不必要的行

   //response.setContentType("text/html");  
     //PrintWriter out = response.getWriter();  
    // out.println("action time"+((ResultSet) request).getString("action_time")+"<br />"); 

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

在您转发到的calendar.jsp中,通过以下方式访问值

<%= request.getAttribute("action_time")%>
<%= request.getAttribute("user_action")%>
<%= request.getAttribute("user_ip")%>
<%= request.getAttribute("user_id")%>
于 2013-02-16T17:50:34.627 回答