从servlet到JSP的输出

时间:2013-02-16 10:16:55

标签: java jsp servlets

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

3 个答案:

答案 0 :(得分:3)

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

从这里引用:What is a request dispatcher and how do I use it?

  

与'include'的情况不同,'forward'丢弃了   Servlet写入响应的先前输出

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

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

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”和“{3}}都有方法getString()。虽然 ResultSet 有这样的方法,但是,在您的情况下,将请求转发给 ResultSet 是没有意义的。


P.S。
HttpServletRequest

答案 1 :(得分:0)

可以将返回的HttpServletRequest请求转发给ResultSet,我认为它应该使用ResultSet的{​​{1}}方法而不进行投射。

getString

答案 2 :(得分: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")%>