We just switched to Glassfish V2. We are now getting errors when setting a session attribute.
Error is:
java.lang.IllegalArgumentException: PWC2788: setAttribute: Non-serializable attribute
Code is:
getRequest().getSession().setAttribute("questionsForUser", getQuestions());
getQuestions() is just a simple getter which is inside an abstract class named 'Parent Action'....so to make getQuestions() a serialized object does my class need to implement serializable?:
public List getQuestions() {
return questions;
}
Edit: I am using ORM (iBatis)
More Information about "Questions"
setter:
public void setQuestions(List questions) {
this.questions = questions;
}
setter is called inside this method. this method calls the iBatis mappings.
public void prepareQuestions()
{
setExamIfaceDAO((SecurityExamIfaceDAO)ApplicationInitializer.getApplicationContext().getBean("securityExamIfaceDAO"));
String userRole = questionsBasedOnUserRole();
int questionsToBeShown = 0;
if (userRole.equalsIgnoreCase("C"))
questionsToBeShown = 15;
else if (userRole.equalsIgnoreCase("U"))
questionsToBeShown = 10;
List local_questions = getExamIfaceDAO().getSecurityQuestions(userRole);
Collections.shuffle(local_questions);
if (local_questions.size()>=questionsToBeShown)
setQuestions(local_questions.subList(0, questionsToBeShown));
getRequest().getSession().setAttribute("questionsForUser", getQuestions());
}
You can't serialise a class that doesn't implement Serializable , but you can wrap it in a class that does. To do this, you should implement readObject and writeObject on your wrapper class so you can serialise its objects in a custom way. First, make your non-serialisable field transient .
In Java, a NotSerializableException exception is thrown when an instance of a class must implement the Serializable interface. The exception is thrown by either the serialization runtime, or by the instance of the class. The argument for the NotSerializableException is the name of the class.
the non-serializable field's Class must have an API to allow getting it's state (for writing to the object stream) and then instantiating a new instance with that state (when later reading from the object stream.)
What happens if you try to send non-serialized Object over network? When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.
You can make the object serializable by using a serializable List implementation and making sure that the objects in the list are also serializable.
Yes -- it is good practice to only put serializable objects in the session since this will allow your application to be run on a server with multiple nodes. Even if you don't care about this at the moment it might be useful in future.
The servlet container should ensure that sessions are not swapped between users. Making objects stored in the session serializable allows the container to 'distribute' the session state across multiple nodes in a cluster, allowing the user who owns the session to have requests serviced by any node.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With