SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast J.Ryan's Avatar
    Join Date
    Jun 2006
    Location
    New Mexico
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problems with asynchronous responses...

    Howdy all,

    I've been working on a site for work and here's how it goes... The user is presented an interface that controls an application on the server using JavaScript (client side) and Java (server side). I'm a bit new to ajax stuff, but I figured it would be straightforward. But I'm kinda dumbfounded by the behavior of my webapp because it's not being consistent... but I think it has something to do with the asynchronicity of my interactions with the server.

    So, the user sends the servlet the command that will cause it to open up a native application running on the server. After a few seconds (I'm using a thread to sleep a couple seconds at a time and then I try to connect - if it fails, I sleep and attempt again until I get success), I would like the servlet to automatically connect to the application via a socket (the application automatically starts listening on the socket after it initializes itself). Meanwhile, I would like the servlet to be sending status messages back to the user so I don't leave him in the dark. This is where the funk starts happening.

    The messages aren't making it back to my webapp, even though I have the statechanged handler for my xmlhttp object reporting any state changes and their accompanying messages. But if I say, send out a dummy request to the server (just asking the servlet to answer back a test message, for instance), sometimes I will get both my test message and one of the dropped messages I was expecting several seconds earlier.

    I thought the whole thing about asynchronous stuff was that the server could do its thing and then report back whenever it wanted.. but that seems to not quite be the case. It's kinda all too slippery for me to grasp.. if anyone knows what's happening and can explain, please go for it!

    Thanks,
    Jess
    Bluesky Saddle Blankets (and other handmade wool stuff)

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    J.Ryan

    Quote Originally Posted by J.Ryan
    Howdy all,
    Meanwhile, I would like the servlet to be sending status messages back to the user so I don't leave him in the dark. This is where the funk starts happening.
    Jess

    There are a few ways to do this. As far as I know you cannot send a response object unless the client is listening for it. It sounds to me like you're trying to poll from the server. What is probable happening is that you keep adding these stat strings into the object, and then when you are legitamately requesting the object it contains your status with it.

    Here is what I would do.

    I would use polling from the client. You have the client browser poll for the status.

    Ex.

    setInterval("statusCheck()",400)

    function statusCheck(){
    //ajax code here which talks to the server
    //assign a method which will capture the server response
    }


    public final synchronized void void serverMethod(HTTPServletObject){
    //query for status here
    //add status to HTTPServletResponse object
    }

    function captureAJAXResponse(data){
    alert(data) //data held the statistic
    }


    Hope this helped...I was a little confused on the problem though

    AFrieze

  3. #3
    SitePoint Enthusiast J.Ryan's Avatar
    Join Date
    Jun 2006
    Location
    New Mexico
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply.. polling is a good idea. I think I'll try that. Another thing I noticed was that Safari (my main browser) seems to have issues when the servlet spawns a thread for its request. I did a simple test where the client pinged the servlet which spawned a thread with a simple response. Safari didn't even recognize that the xmlhttp object even had any responseText. Interesting. Firefox works fine under the thread test... Now, you'd think they'd handle similarly since they both are technically mozilla based browsers.

    Thanks!
    -Jess
    Bluesky Saddle Blankets (and other handmade wool stuff)

  4. #4
    SitePoint Enthusiast J.Ryan's Avatar
    Join Date
    Jun 2006
    Location
    New Mexico
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, just to keep you updated.. when I do some polling, even as little as once every 2 seconds, I get a java.lang.NullPointerException from the server. When I poll via the POST method, I get it right away (ie: as the response to my first poll after the initial user request), if I poll via the GET method, I noticed it replied a few times and then threw the exception.. saying: my.package.Class.doPost (Unknown Source). (and of course the GET method returned the same but with .doGet).

    What is causing this exception to be thrown? The class and method obviously exist because they work the first time I access them... and also they work for subsequent user requests, just not the repeated polls...

    -Jess
    Bluesky Saddle Blankets (and other handmade wool stuff)


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
  •