0

Hi I have been trying to do session replication between my 2 tomcat clusters...and also I am maintain sticky session...I am currently having a problem that after adding tag in web.xml and running my application it gives an error like **java.lang.IllegalArgumentException: setAttribute: Non-serializable attribute** and when I referred the tomcat documentation for finding solution it was mentioned that All your session attributes must implement java.io.Serializable

...and I get doubt here all my session attributes must implement serializable means that all the objects I am storing in the session using setattribute must implement serializable interface...that mean if I am doing

public class MySession {
  String sessionObj1 = "Hello";
  HttpSession session = request.getSession();
  session.setAttribute("New-Session-1", sessionObj1);
}

Here sessionObj1 is a String Object and serializable likewise I should set only objects that implements serializable interface

  • Your code doesn't make sense since it looks like a method body but it's just at class level. In general implementing `Serializable` should be sufficient as long as all _fields_ in a class do so too. `String` already implements that interface so if `sessionObj1` were a field that would be ok (of course the other lines wouldn't even compile). – Thomas Jan 11 '23 at 07:05
  • I just wrote that code as rough code here...to get clarified...I have another small doubt that is how do I make my JSONObject serializable...I am also setting a session object of org.json.JSONObject class here this JSONObject is not serializable...I cannot can't also change this JSONObject to string as mentioned in other answer given to this question related to this in stack overflow – Ganapathy R Jan 12 '23 at 03:52
  • @Thomas and tnx by the way for answer – Ganapathy R Jan 12 '23 at 03:53
  • Well, since JSONObject isn't serializable you need to take other measures, i.e. either just store the json string, parse the json into a proper pojo that is serializable, or add custom serialization and deserialization code to your session. – Thomas Jan 12 '23 at 07:08
  • Bro my problem is I am not allowed to modify the session.setAttribute(jsonObj)...so I am un able to set jsonobject to string...is there any other ways that I can use to serialize the json object that is being set at session – Ganapathy R Jan 12 '23 at 11:26

0 Answers0