Friday 3 February 2012

SEQUENTIAL IMAGE LOADING FROM SERVER USING JSON & jQUERY

  Today, I successfully developed a demo for getting pictures one by one from the server automatically, by timer effect.   I made use of JSON  and jQUERY.
     This time, I have made use of JSON-Java library as a jar file. I got that from JSON.org site.
The name of the jar file that I selected is json-simple.jar. There are a lot of json-java librarries there!.
   This library is recommended in  Ajax on Java book. which I am using.
I placed this jar in tomcat\web-apps\root\web-inf\lib  folder.

--------------------------------------------------------------------------------
The jsp code is given below.


 JSONObject             ob1  =  new JSONObject();
       JSONObject       ob2  =  new JSONObject();

      String s1="";

      s1="alice.jpg";      ob1.put("alice",s1);

      s1="clinton.jpg";      ob1.put("clinton",s1);

      s1="cindy.jpg";      ob1.put("cindy",s1);

      s1="colin.jpg";      ob1.put("colin",s1);

      s1="nathan.gif";      ob1.put("nathan",s1);

      s1="mathew.gif";      ob1.put("mathew",s1);

      ob2.put("myarray",ob1);      String r=ob2.toString();


      System.out.println(r);  // for checking
      out.println(r);
----------------------------------------------------------------------------------------
 Now to the client side.


function   job5()   // jQuery Ajax
     {
      $.get("imagesender.jsp",function(r)
        {     
       a=r
      });
              a = eval('('+a+')');             
                    }

n=1
     function   job1()
     {
       if(n==7){         n=1}

       if(n==1){          s="http://localhost:8080/"+a.myarray.alice}  etc
       if(n==2)            s="http://localhost:8080/"+a.myarray.clinton
     

       setTimeout("job4()",1000)

     }
     function  job4()
          {
       $("#img1").attr({src:s});
        n=n+1
       job1();
     }
     //------------------------