Search code examples
javajava-web-startserializable

Why we need implement Serializable for classes put in server when create a java web


I am learning a java web and my teacher makes me must implement Serializable for classes put on the server. Why we must do it. Because when I do not implement Serializable my server still active. So, who can answer me that Why I have to implement Serializable class for classes put on the server-side


Solution

  • I assume we are talking about web apps built on Jakarta Servlet technology.

    Your web app takes up memory on the web container at runtime. The more simultaneous users, the more memory is taken. To alleviate pressure on memory, some web containers invoke a feature whereby the less-active user sessions are written out to disk.

    Also, some web containers do this when shutting down. Current sessions still running at shutdown are saved to disk, and automatically reconstituted when the server launches again.

    Yet another scenario is load-balancing where multiple web containers working in tandem may relocate some sessions from any over-burdened server to any underutilized server. This transfer occurs on-the-fly during runtime.

    In all these cases, the objects of your web app classes must support Serializable so that they may be written to storage or communicated over the local network.

    In some complicated apps, making all your objects serializable may not be feasible. In that case you may choose to disable those three features I discussed above. Then you would make sure to run your web app on a deployment server that has more than enough cores and more than enough memory to handle the peak number of users your web app will encounter.