How do I serialize objects?

Hi,I have to pass a parameter to an ejb method which is an instance of ServletOutputStream. However, this class is not serializable so how can I make it so that it can be passed successfully?Thanks!!
[220 byte] By [Mr_RKa] at [2007-10-2 9:34:37]
# 1

Is the source for the class that isn't serializable under your own control? In that case you declare it to implement Serializable. Read about Serializable in the API docs for more details.

If you have no access to the source, you are out of luck. You can't use Java serialization with classes that aren't Serializable (with the exception of a stateless superclass with a no-arg constructor which I doubt is relevant to your case if you are using it as a parameter).

Lokoa at 2007-7-16 23:40:52 > top of Java-index,Core,Core APIs...
# 2

Thanks for the response.

The class in question is the class returned by HttpServletResponse.getOutputStream()

.

The object returned is of type ServletOutputStream.

Can I not just subclass ServletOutputStream and make the subclass implement the serializable interface or is this not how serialization works.

Mr_RKa at 2007-7-16 23:40:52 > top of Java-index,Core,Core APIs...
# 3
> Can I not just subclass ServletOutputStream and make> the subclass implement the serializable interface or> is this not how serialization works.Streams are never required to be serialized.
aniseeda at 2007-7-16 23:40:52 > top of Java-index,Core,Core APIs...
# 4

It sounds like you have a big design flaw in your application.

You want to pass a ServletOutputStream from a servlet (or JSP) to an EJB, so that the EJB can write data to the output stream.

That's not a good idea.

EJB's do not necessarily run on the same server as the servlet. What would happen if you deploy your application on a cluster, and a servlet running on server A calls an EJB that happens to run on server B?

The servlet would try to pass the output stream to the EJB, and the EJB would try to write data to the output stream. But there's no way that the data that the EJB writes to the output stream gets back to the servlet container running on server A. So this design will not work.

This kind of design also violates the MVC paradigm - you normally don't implement the View part of MVC in an EJB.

So, you should reconsider the design of your application.

jesperdja at 2007-7-16 23:40:52 > top of Java-index,Core,Core APIs...
# 5

> Can I not just subclass ServletOutputStream and make

> the subclass implement the serializable interface or

> is this not how serialization works.

You can subclass it, and your subclass would be Serializable. But the base class part, which holds the relevant data, is not serialized. At deserialization, the base class is restored using its default constructor if it has one, and throws an exception if it hasn't.

This is explained in the API and can easily be verified with simple test code.

Lokoa at 2007-7-16 23:40:52 > top of Java-index,Core,Core APIs...