Prabhat
Prabhat

Reputation: 2263

Store java objects in HTML5 local storage

I want to store objects I get from request.getAttribute() from previous JSP in HTML5 local storage so that I can use it in another page, since I dont want to use server session to store attribute. How to achieve this ? The object I want to store has 5 strings in it with getter and setters.

Upvotes: 0

Views: 14052

Answers (1)

ace
ace

Reputation: 6825

You can achieve this by using the localStorage setItem and getItem using JavaScript.

To add the request paramter to HTML5 local storage.

<script type="text/javascript">
    localStorage.setItem('user','${param.user}'); //Where param user is your request parameter from previous jsp.
</script>

Now to get the value you store to local storage

<script type="text/javascript">
    localStorage.getItem('user');
</script>

Note: This will only work to a browser that support the localStorage.

Check html5/webstorage for more information.

UPDATE: The example above use string to store in a localStorage. To achieve storing as an object this will need some convertion or using a mixed of available tools. Like Java object to json to javascript to localstorage or json to local storage. I have not yet tested this but some links below may help to do this

Note: This could not be the exact way to store Java object in localStorage or there can't be anyway. But I think it is possible to store the Java Object in localStorage using some of the techniques above. To get the object in the localStorage, I think you need a code the will parse the object on the serverside(Java).

Upvotes: 4

Related Questions