• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflect the new value of a session variable updated via AJAX.  RSS feed

 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

On a JSP page, on click of a button to show some content in a modal div ( a <div> behaving like a modal dialog box) , I'm updating a session variable after doing some internal check via AJAX.

My problem is, when this modal div is dismissed, I'd like the currently open, 'underlying' JSP page to get the updated value of the session variable without refreshing the JSP page (which was the whole point of using AJAX in this case).

But when I read the session variable using Javascript, I see that it still has the old value of the session variable. On refreshing the page though, everything falls in place and the JS alert displays the updated value. But refresh is what I'm trying to avoid.

Any help is highly appreciated.

Thanks!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You *should* be able to use the return from the Ajax call to update the underlying div--how are you handling the callback?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The session is a server-side concept. Nothing in the world is going to make JSP code re-run itself on the page without a refresh. You can't rely upon any JSP mechanism after your Ajax call.

As Steve suggested, you'll need to return any information you need as the Ajax response, and use JavaScript to update the page.
 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was a kinda aware of that, but was hoping the following scenario was possible to implement.

Continuing with my reply to the 2nd post,

My callback method is mainly being used for:

1) Updating the contents of the modal div and wait for the user to dismiss it. OR
2) On satisfying certain conditions, dismiss the modal div automatically.

Once the UI part is taken care of (which is working fine right now), I'd like for this to happen:

1) When the user resubmits the JSP which was already open, I'd like my javascript method (which is being called on form onsubmit), to be able to find out that the session variable was updated between the time the page was loaded and the time of submission.

From another forum, I got an idea to use my onsubmit javascript to make another AJAX call to find out the value. And then prevent/allow submit based on the response. I'm trying that out now, and don't see any obvious reason why it won't work. But personally, I find this clumsy and AJAX overkill. At this point, I'm a little averse to reconsider the whole design itself.

Please let me know if you need more information about this scenario.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good instinct. I think sitting back and re-thinking the design might be a good idea at this point -- it just sounds like you've got too many moving parts.
 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm not sure about the too many moving parts. But I feel I might have over-complicated the implementation. Have been working on this for quite sometime so I apologize if I'm failing to see some very simple and obvious solution.

The original piece of requirement for this issue was:

1) Show the various pages to the user no matter what.
2) Upon trying to act on the page in anyway that would update the database, verify if certain tasks have been performed already.
3) If not, prompt them to do it (hence the modal div and ajax) and unless they do it, don't let them submit.
4) If the user satisfies the requirements using the modal div, dismiss the div and record the completion of requirements.
5) Then when a submit is re-attempted, verify and allow!

"SIMPLE", I'd have been inclined to think under other circumstances...!

The pages are allowed to be shown no matter what and the user can enter data as well. Only on submit does the requirement to satisfy certain preconditions arises. Refresh is avoided, so that no data that is already entered on the page is lost and also to avoid the user being taken to any other page.
 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My 'not-so-elegant' approach in the 4th post in this thread worked.
Thanks everyone!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!