how to pass image from servlet to applet?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: how to pass image from servlet to applet?

  1. #1
    Join Date
    Jun 2005
    Posts
    2

    how to pass image from servlet to applet?

    I am writing a servlet/applet program that displays a http image. e.g. http://10.10.1.100/png/image1.png

    since the image is not on the server that host the servlet, seems like the sandbox policy restricted me from displaying the image directly on my applet using getImage() function . So, I would like to ask for your help in preparing the servlet and applet codes for the servlet to pass the image to applet, and then the image is displayed on applet.

    I am a new comer in Java programming, can you please give me the instruction as detail as possible? Thank you very much.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I've done that, some years ago. What I used was a servlet that received the url
    of the image, got the image as a byte stream over the net, converted it into a gif using a gifencoder and send it back to the client.
    Using the servlet was easy, it was included in the html as the target of the src
    parameter on image tags.

    So it did not send the image to the applet as you want, but I guess that could be
    fixed as the applet could get the byte stream and to the image create itself...

    I could dig up the code for you if you like.
    eschew obfuscation

  3. #3
    Join Date
    Jun 2005
    Posts
    2
    Hi sjalle,

    Thank you very much for your help. I actually understand the logic of what to do with it, but I really don't know how to do it. It would be much helpful if you can give me the code. I desperately need your help. Thank you veyr much.

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I found it, I think...

    Here is an applet and a servlet, the applet gets image url as user input
    and calls the servlet with the image url as the only parameter. The
    applet then creates an image from the byte stream returned from the
    servlet

    NOTE

    I have not tested this, as my server setup is not quite ok at the moment.


    Update:

    Uuuups ! I have tested the applet and found that i had forgotten something
    important:
    Code:
    public void init() {
    
          jbInit();
          getURLBtn.addActionListener(this); // << essential stuff
    
      }
    Ive tested it without the servlet, just writing the image url in the url entry
    field, and that works. The servlet is basically doing the same thing as the
    applet, except that it accesses other servers and returns the image byte
    stream to the applet.

    PS: the GifEncoder I mentioned is not required for plain image transfer.
    Attached Files Attached Files
    Last edited by sjalle; 06-08-2005 at 09:06 AM. Reason: deaf button
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center