Search code examples
javascripthtmljspsubmit-button

How to create a JSP variable that does not reset on page reload


I have two buttons, one called "Previous" the other is called "Next". When they are clicked, they reload the JSP page in which they are defined. They are also supposed to either increment or decrement a variable.

<form name="input" action="MyPage.jsp" method="post">
<html> 
<input type="submit" value="Previous" onclick="<% hitsCount--; %>"> 
</html> 
<html>
<input type="submit" value="Next" onclick="<% hitsCount++; %>">
</html> 

The problem I am having is that I cannot figure out how to define the variable in such a way that it is not reset each time the page loads.

For example if "hitsCount" is 0, and I click the "Next" button the page should reload and the value of "hitsCount" should be incremented by one. The next time I hit the "Next" button, the page reloads and "hitsCount" should contain a value of 2.


Solution

  • you can use session storage for keep the values in the client side

    Here is a link where you can see how session storage works.