0

I called my servlet from Jsp file and here my servlet (i am setting a session name, value) which is used to output data in a div tag of same jsp page who called servlet. i am using that session value as a hidden field in the form submit to jquery, but here, the updated session value is not reached in jquery??

Any solution for the same??

and yes, the jquery fetches the updated session value, if i reload the page before submitting the page..

code

servlet file:

HttpSession session = request.getSession(true);
       session.setAttribute("totalpurchase", total);

jsp file:

<form method="post" action="" onclick="check()">
            <input type="hidden" value="${sessionScope.totalpurchase}" id="totalpurchase">
</form>

    <script>
function checkcart() {
var totalpurchase = document.getElementById('totalpurchase').value;
    alert(totalpurchase);
}
    </script>
Shivam
  • 33
  • 1
  • 9
  • Can you please give code snippet? – user3657302 Jul 07 '14 at 17:22
  • yes, but i am trying things from my side. and thats why code gets a bit changed.. – Shivam Jul 07 '14 at 17:30
  • Below link may help you understand relation between session and sessionScope: http://stackoverflow.com/questions/3345180/which-scope-application-servletcontext-httpsession-will-el-use-for-interpret Please confirm both sessions on page and servlet are same. and then proceed. – user3657302 Jul 07 '14 at 17:53
  • What else are you doing in Servlet? How you are coming back to JSP from Servlet? Do you want to update the div value without refreshing the page? – Braj Jul 07 '14 at 19:29
  • @user3657302 : yes both sessions are same.. – Shivam Jul 08 '14 at 03:31
  • @Braj : Actually nothing just simply putting things in the cart's div tag and creating the totalpurchase amount as in the session, to use it in the jsp page .. – Shivam Jul 08 '14 at 03:33
  • I would suggest go with scriptlet and print values on console to confirm it is being added. Add SOPs just after adding to session with timestamp in SOP and inside JSP. Compare both values, on first load and on refresh. If you will print value from session, then you will get total 4 values along with timestamp, you will come to know when those values were isnide session and when those are lost. – user3657302 Jul 08 '14 at 14:14
  • i checked the same. session.getAtribute("totalpurchase") is working on servlet page with updated value, but its scriptlet version is not responding in jsp page and it only responds using ${sessionScope.totalpurchase} with previous value, but after a page refresh it gets the updated new value and show it.. – Shivam Jul 08 '14 at 17:11

1 Answers1

0

session and sessionScope are two different objects. You can read about it on this page.

One way is directly using ${totalpurchase}. This has limitation that no other attribute similarly named is not been used.

user3657302
  • 347
  • 1
  • 5
  • `${sessionScope.totalpurchase}` is the correct way to get the attribute from session scope. `${session.totalpurchase}` is incorrect. Read the link that you have shared in your post. – Braj Jul 07 '14 at 19:26