Search code examples
javaxpagessession-storage

How to get a handle to the sessionStorage using JAVA


I'm looking at a situation that requires a fair bit of data storage that I have considered using sessionScope variables for. In a small test that I ran this worked well, but the data does get written back to the server with each change to the data. I'm considering creating a JAVA bean to update and control the structure of this data and store it in a series of sessionStorage variables. The sessionStorage is easy in CSJS but I'm not sure how to access these variables from a JAVA Bean. The application is something like a shopping cart so I would maintain the data in the sessionStorage until the document submit, then load the values into the document and save it and I think this would work best as a JAVA.


Solution

  • The sessionStorage object stores the data for one session in browser window/tab on client side.

    There is no way to access the sessionStorage from server with Java.

    You can though send the data to server and there you can pick it up with Java.

    You can send it per Ajax or you can submit it in hidden fields.

    If you store a complex data structure with objects and arrays in sessionStorage then you might convert it to JSON as an easy to use transport medium.

    You would convert the sessionStorage to a JSON string on client side with

    JSON.stringify(sessionStorage)
    

    You can parse the JSON string with Java on server side with classes from com.ibm.commons.util.io.json package

    JsonJavaFactory factory = JsonJavaFactory.instanceEx;
    json = (JsonJavaObject) JsonParser.fromJson(factory, jsonString);