Search code examples
javascriptjspstruts2session-variables

Updating session variable in JavaScript function in Struts 2


Would like to know the ways to update the session variable in JavaScript function when the submit button is clicked and the same variable to be accessed in controller class.

The below links provides information on setting session variable in function

Pass Javascript Value to Java in JSP

and

JSP - how to pass a javascript var in session.setAttribute?

But, I need to update session variable only when the checkbox is selected and pass to java controller on form submission.


Solution

  • The session variable in javascript function can be updated only if you update the variable on the server. But since the javascript is executing in the browser you can't update the session variable.

    Then you need update the page where this session variable is used.

    If you can't update the part of the page and need to update the whole page to make changes effective then send redirect result with the response that refreshes the page. It will reinitialize the javascript with the variable in session.