47
votes

I am not able to understand what is the function of this line in web.xml

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

I have read that the NetBeans default is client. I've just faced an issue that I have many beans in my application, and the <param-value> was set to client, so I was getting

java.io.NotSerializableException

error although my beans were Serializable (i.e. they implemented the Serializable interface.). My beans were in @ViewScope. But when I changed it to server, things are going to work. Why? What is the difference when I use client and server. Can anyone explain me with the help of an example.

Thanks

2

2 Answers

58
votes
java.io.NotSerializableException

This kind of exception has usually a message in the root cause which shows the fully qualified class name of the class which doesn't implement Serializable. You should pay close attention to this message to learn about which class it is talking about and then let it implement Serializable accordingly.

Often, making only your managed bean classes serializable is not always sufficient. You also need to ensure that each of its properties is also serializable. Most standard types like String, Long, etc implement all already Serializable. But (custom) complex types such as nested beans, entities or EJBs should each also be serializable. If something is not really implementable as Serializable, such as InputStream, then you should either redesign the model or make it transient (and keep in mind that it will be null after deserialization).


What is the difference when i use client and server

First some background information: Why JSF saves the state of UI components on server?

The main technical difference is that the client setting stores the entire view state as the value of the javax.faces.ViewState hidden input field in the generated HTML output and that the server setting stores it in the session along with an unique ID which is in turn referenced as the value of the javax.faces.ViewState hidden input field.

So, setting to client increases the network bandwidth usage but decreases the server memory usage and setting to server does the other way round. Setting to client has however an additional functional advantage: it prevents ViewExpiredExceptions when the session has expired or when the client opens too many views.

12
votes

javax.faces.STATE_SAVING_METHOD parameter is used to specify where the state should be saved.

If you want to save the state on the server (which is the default in the JavaServer Faces reference implementation), specify the param-value value as server.

Otherwise to save the state on the client side we can specify client in the param-value.

If the state is saved on the client, the state of the entire view is rendered to a hidden field on the page.