File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes When to use Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "When to use Serializable" Watch "When to use Serializable" New topic
Author

When to use Serializable

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Gurus,
I have some doubts regarding when to use the interface serializable. I have a very simple application. There is a servlet that invokes some bean. The bean does the jdbc connections and gets a ResultSet. The value object class is initialized from the ResultSet. Now my question is do i need to serialize the value object. If not when would i need serialization in such web applications?

Thanks,
Martin
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Martin,

Firstly, I'm no guru, so what you read below should be taken as opinion only.

So, your question is should you implement java.io.Serializable on your value object. It's certainly a reasonable thing to do, but whether you actually do it or not might depend on the answer to your second question.

Do you need to serialize the value object? Well, only you can answer that. You could serialize your object if you wanted to transmit its state, say over a network. You could also use serialization if you wanted to persist your object's state, say to a file or database. The benefit of serialization is that you can easily deserialize the object at the other end, or at a later date.

Typically in my experience of using web applications with relational databases there's no need to use object serialization but I expect it depends on your architecture and requirements.

Hope that helps.

Jules
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Broadly, two major cases.
  • When your web application is distributable, the application server needs to be able to replicate sessions between servers by serializing the objects in the session.
  • When your web application needs to marshal domain objects over the network, for example to communicate with a remote EJB tier.
  • - Peter
    Gerome Kawa
    Ranch Hand

    Joined: Dec 05, 2002
    Posts: 61
    I think its a good idea to serialize the value object if you are not sure if you need it. Its not going to harm your application.
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    KISS, YAGNI.

    - Peter
    Martin Lira
    Ranch Hand

    Joined: May 26, 2004
    Posts: 97
    Gotit gurus!!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: When to use Serializable