1

我需要使用 JDBC 持久性来管理服务器会话。现在我需要将会话值(保存在 wl_session_values 中)变成一个真实的对象。下面我写我的代码:

try {
String userName = "root";
String password = "****";
String url = "jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);

PreparedStatement pstmt = null;
String query = "select * from wl_servlet_sessions";
pstmt = conn.prepareStatement(query);
ResultSet resultSet = pstmt.executeQuery();

while(resultSet.next()){
    System.out.println(resultSet.getString("wl_id"));

    Blob blob = resultSet.getBlob("wl_session_values");
    InputStream in = blob.getBinaryStream();
    ObjectInputStream ois = new ObjectInputStream(in);

    Hashtable<Object, Object> hash = (Hashtable)ois.readObject();
        for(Map.Entry<Object, Object> newHash : hash.entrySet()){
        System.out.println("Key : " + newHash.getKey().toString());
        System.out.println("Value : " + newHash.getValue().toString());
    }
}

} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (conn != null) {
    try {
        conn.close();
    } catch (Exception e) { e.printStackTrace(); }
}

但是当我尝试反序列化结果时,我总是得到java.io.StreamCorruptedException: invalid stream header: 73720013.

谢谢你的帮助。

4

1 回答 1

1

这些是私有 API,所以我会远离。

您可以像本示例中那样解决实现HttpSessionListener的问题。

于 2011-01-17T16:13:36.047 回答