How to retain the form values after refresh in jsp?

forms, jsp

Solution

I finally found an answer for this question. To temporarily store the form values we can use the `localStorage` of Javascript. Below is an example which explains how to use it,

<!DOCTYPE html>
<html>
    <head>
        <script src="js/jquery.js"></script>
        <script>
            $(document).ready(function(){
                document.getElementById("io").value = localStorage.getItem("item1");
            });
        </script>
        <script>
            $(window).on('beforeunload', function() {
                localStorage.setItem("item1",document.getElementById("io").value);
            });

        </script>
    </head>

    <body>
        <form>
            <input type="text" id="io" name="io">
            <input type="submit">
        </form>
    </body>

</html>

Run the above code and type something in the textbox. Now even if the page is refreshed the value within the textbox will persist i.e. will not change.

Problem

I have read many topics regarding this issue but almost all of them aims at how to retain the values of the form once the form is submitted. But in my case I don't want to submit the form.I'm having 3 checkboxes and based on the selection of the checkbox by the user some values are displayed dynamically without submitting the page. The default values of the checkboxes are chk1val1 chk2vala chk3val1. Now if the user makes the selection as chk1val2 chk2val3 chk3val2, then even after the user refresh the page I want to retain that selection done by the user. Any pointers on this one?? Thanks in advance.

Original source