SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: AJAX and JSP

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX and JSP

    Hi All,

    I have a jsp which displays a list read from database. In database, status field is dynamically updated by background thread. I want to display the changed status without refreshing page. So, I am using AJAX. I want to make AJAX request conditionally. I mean if all the statuses on the page are complete, then I don't want to make AJAX call, because there is nothing to update. I want to make AJAX call only if I have status other than complete. From my servlet, I set a flag in a request attribute as true or false dependng on status. In a JSP I get request attribute assign it a js variable. If it's true make AJAX call and I call that function on body onload. It works fine for the first time. But js variable never gets updated even though sevlet updates flag value. How do I address this issue? Calling function on onload will not work.

    Any help is appreciated.

    Thanks,

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets see the javascript generated by your JSP then

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function autoUpload() {
         var flag = <%= request.getAttribute("istrue") %>
         if (flag == "true") {
             new Ajax.PeriodicalUpdater('top', 'DisplayList.do', {method:'post',frequency:60,decay:2});
         }
    }

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (flag == "true")

    This is looking for the value of flag to be a string - "true". But you're probably creating a boolean using

    var flag = <&#37;= request.getAttribute("istrue") %>

    (unless the jsp is adding quotes)

    Try:
    if (flag) {

    instead

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah. Now, I changed it to null. I mean I'm setting attribute value only if condition is true otherwise it's null. In js my condition looks like this -
    if (flag != null)

    But, didn't work

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try putting an alert inside the condition, just to see if it's working.

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •