日期:2014-05-20  浏览次数:20831 次

我的这个代码取不到数据 而且回来的页面全是问号 怎么回事!!!
Java code

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>" />
        <meta http-equiv="content-type" content="text/html; charset=utf8">
        <link rel="stylesheet" type="text/css" href="css/manageRecord.css">
        <script language="javascript" src="js/calendar.js"></script>
    </head>

    <body>
        <form action="ManageRecord.do" method="post">
            <div class="all">
                <span class="title">聊天记录管理</span>
                <div class="content">
                    选择客服:
                    <input type="text" name="chatId">
                    <br>
                    对话时间:
                    <input type="text" name="time1" id="time1"
                        onclick="MyCalendar.SetDate(this)" />
                    至
                    <input type="text" name="time1" id="time2"
                        onclick="MyCalendar.SetDate(this)">
                    <br />
                    <input type="submit" value="查询" style="margin-top: 15px;">
                </div>
                <div class="count">
                    <div id="c1">
                        <table width="800" border="1">
                            <tr>
                                <th>
                                    客服名称
                                </th>
                                <th>
                                    聊天内容
                                </th>
                                <th>
                                    聊天日期
                                </th>
                            </tr>
                            <tbody>
                                <c:forEach items="${chatRecords}" var="chatRecord">
                                    <tr>
                                        <td>
                                            ${chatRecord.serverName }
                                        </td>
                                        <td>
                                            ${chatRecord.chatContent }
                                        </td>
                                        <td>
                                            ${chatRecord.chatCreateTime }
                                        </td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>
                    </div>
                    <div class="c2">
                        <input type="checkbox" />
                        全选&nbsp;&nbsp;
                        <input type="button" value="删除" />
                    </div>
                </div>
            </div>
        </form>

    </body>
</html>

package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.ChatRecord;

import bo.ChatRecordBo;

public class ManageRecord extends HttpServlet {
    private static final long serialVersionUID = -8402572450669973831L;
    private ChatRecordBo chatRecordBo = ChatRecordBo.getInstance();
    
    public ManageRecord() {
        super();
    }

    public void destroy() {
        super.destroy();

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        int chatId = Integer.parseInt(request.getParameter("chatId"));
        String time1 = request.getParameter("time1");
        String time2 = request.getParameter("time2");
        List<ChatRecord> chatRecords = chatRecordBo.getRecords(chatId, time1,
                time2);
        response.getWriter().print(chatRecords);
        if (chatRecords != null) {
            request.setAttribute("list", chatRecords);
            request.getRequestDispatcher("/adminFunction/manageRecord.jsp")
                    .forward(request, response);
        } else {
            response.sendRedirect("/index.jsp");
        }
    }

    public void init() throws ServletException {

    }

}
package dao;

import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import db.DBConnection;
import entity.ChatRecord;

public class ChatRecordDao {
    private ChatRecordDao() {
    }

    private static ChatRecordDao instance;

    public static ChatRecordDao getInstance() {
        if (instance == null) {
            instance = new ChatRecordDao();
        }
        return instance;
    }

    public List<ChatRecord> getChatRecords(int chatId, String chatCreateTime1,
            String chatCreateTime2) {
        ArrayList<ChatRecord> chatRecords = new ArrayList<ChatRecord>();
        String sql = "select * from chatRecord where chatId = ? and chatCreateTime  between ? and ?";
        Connection conn = DBConnection.getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, chatId);
            pstmt.setString(2, chatCreateTime1);
            pstmt.setString(3, chatCreateTime2);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                ChatRecord chatRecord = new ChatRecord();
                chatRecord.setChatId(rs.getInt(1));
                chatRecord.setServerName(rs.getString(2));
                chatRecord.setChatContent(rs.getString(3));
                chatRecord.setChatCreateTime(rs.getDate(4));
                chatRecords.add(chatRecord);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConnection.closeAll(rs, pstmt, conn);
        }
        return chatRecords;
    }
}