You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Johan Wasserman - CPX Mngd Services <Jo...@Comparex.co.za> on 2004/04/16 08:27:26 UTC

[OT] Serializing HttpSession Object

I need to serialize the session and save it to a blob in MySql (i use
Hibernate).
 
I have tried, for example;
...
ByteArrayOutputSream baos = new ByteArrayOutputstream();
ObjectOutputStream oos = new ObjectOutputStream(boas);
oos.writeObject(session);  //<-- it fails here, nothing in the logs
byte[] sessionAsBytes = baos.toByteArray();
...
 
I need this to give the user the option to restore a previous session
(where they where before they last logged out, with all the session
variables required to restore to that point).
 
 Thanks, Johan.

Re: [OT] Serializing HttpSession Object

Posted by Michael McGrady <mi...@michaelmcgrady.com>.
I don't know you and there is not enough code here to tell how much you 
know, but do you know that you have to get the output stream to write to 
from wherever you are sending it to?  Thus, you might have something like:


       URL url = new URL(/* some destination */);
       HttpURLConnection connection = (HttpURLConnection)url.openConnection();
       connection.setDoInput(true);
       connection.setDoOutput(true);
       connection.setUseCaches(false);
       connection.setRequestProperty("Content-Type", 
"application/x-java-serialized-object");
       ObjectOutputStream out = new 
ObjectOutputStream(connection.getOutputStream());
       out.writeObject(message);
       out.flush();
       out.close();
       BufferedInputStream bis = new 
BufferedInputStream(connection.getInputStream());
       Reader reader = new InputStreamReader(bis);
       StringBuffer sb = new StringBuffer("");

       int read;
       byte [] buffer = new byte[1024];
       while ((read = bis.read(buffer)) != -1) {
         sb.append(new String(buffer));
       }

Is this helpful?  Why not use your logger to find out what is up?




At 11:27 PM 4/15/2004, Johan Wasserman - CPX Mngd Services wrote:
>I need to serialize the session and save it to a blob in MySql (i use
>Hibernate).
>
>I have tried, for example;
>...
>ByteArrayOutputSream baos = new ByteArrayOutputstream();
>ObjectOutputStream oos = new ObjectOutputStream(boas);
>oos.writeObject(session);  //<-- it fails here, nothing in the logs
>byte[] sessionAsBytes = baos.toByteArray();
>...
>
>I need this to give the user the option to restore a previous session
>(where they where before they last logged out, with all the session
>variables required to restore to that point).
>
>  Thanks, Johan.

Re: [OT] Serializing HttpSession Object

Posted by Michael McGrady <mi...@michaelmcgrady.com>.
I just did a quick check and noticed that the session object is not 
serializable.  Have you checked out the RequestInfo object and its 
ilk?  Why don't you replace the current sessions with a set of session 
objects like the EJB 2.0 CMP entity beans?


At 11:27 PM 4/15/2004, Johan Wasserman - CPX Mngd Services wrote:
>I need to serialize the session and save it to a blob in MySql (i use
>Hibernate).
>
>I have tried, for example;
>...
>ByteArrayOutputSream baos = new ByteArrayOutputstream();
>ObjectOutputStream oos = new ObjectOutputStream(boas);
>oos.writeObject(session);  //<-- it fails here, nothing in the logs
>byte[] sessionAsBytes = baos.toByteArray();
>...
>
>I need this to give the user the option to restore a previous session
>(where they where before they last logged out, with all the session
>variables required to restore to that point).
>
>  Thanks, Johan.

Re: [OT] Serializing HttpSession Object

Posted by Craig McClanahan <cr...@apache.org>.
Johan Wasserman - CPX Mngd Services wrote:

>I need to serialize the session and save it to a blob in MySql (i use
>Hibernate).
> 
>I have tried, for example;
>...
>ByteArrayOutputSream baos = new ByteArrayOutputstream();
>ObjectOutputStream oos = new ObjectOutputStream(boas);
>oos.writeObject(session);  //<-- it fails here, nothing in the logs
>byte[] sessionAsBytes = baos.toByteArray();
>...
> 
>I need this to give the user the option to restore a previous session
>(where they where before they last logged out, with all the session
>variables required to restore to that point).
> 
> Thanks, Johan.
>
>  
>
You are likely to fail at serializing the HttpSession object itself, 
because the container makes no guarantees that the implementation class 
will be Serializable.  What you might want to consider is just saving 
and restoring the session attributes themselves.

Craig


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org