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.
1 Answers
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).
-
you can't store Java-Objects via, JavaScript! – Hache May 10 '11 at 06:02
-
@Hache: you have a point, I updated my post. I guess there's still a way to store the Java Object into the localStorage. Anyway thanks for pointing out. – ace May 10 '11 at 06:34
-
@Hache: it's definitely possible. They will only be converted to String. So if they are String already of you have a suitable converter to get them back, then there's no problem. – BalusC May 10 '11 at 11:53
-
if I talk about objects, I mean real objects with methods and so on, not only the data, since he's talking about getter&setters he seams to mean that to. Sure it's possible to serialize objects an deserialize them later. The possibility ace pointet out only works with simple datatype not with complex objects – Hache Jun 21 '11 at 06:19