Applet Servlet Communication


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Applet Servlet Communication

  1. #1
    Join Date
    Oct 2005
    Posts
    16

    Applet Servlet Communication

    Dear friends,

    when i call a Servlet from a Applet . it give IOException .


    it showing this message also " Server returned HTTP response code: 405 for

    the URL : url of Servlet "

    when I call the Servlet in a normal way it works.

    please help me

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

    Check this code

    This method loads a file (named listName) from the server directory "system" containing a list of image file names
    and then loads these images. The class used for connecting (SiteConn) is at the end of this reply.

    Note: this method executes in a thread.

    Code:
    private void loadImageSequence() throws Exception {
        String hostName=applet.getDocumentBase().getHost();
        String p=applet.getDocumentBase().getPath();
        int ix=p.lastIndexOf("/");
        String pth="http://"+hostName+"/"+p.substring(0,ix)+"/system/"+listName;
    
        URL url = new URL(pth);
        System.out.println("url: "+url.toString());
        SiteConn sc = new SiteConn(url); 
        String s = sc.getContents().toString();
        System.out.println("content: "+s);
        String [] ss=s.split("\n");
        MediaTracker mt=new MediaTracker(applet);
        int n=0;
        imgList.clear();
        for (int i=0; i<ss.length && this.running; i++) {
          url=new URL("http://"+hostName+"/"+p.substring(0,ix)+"/images/"+ss[i]);
          Image img=applet.getImage(url);
          mt.addImage(img,n++);
          imgList.add(img);
        }
        // prepare for double buffering
        memImg=new BufferedImage(applet.getWidth(), applet.getHeight(),BufferedImage.TYPE_INT_RGB);
        mt.addImage(memImg,n++);
        try {
          mt.waitForAll();
        } catch (Exception ex) {
          msg="Image load fail: "+ex.getMessage();
        }
        memG=memImg.getGraphics();
        repaint();
      }
    The SiteConn class
    Code:
    /**
    Usage example:
    try {
      URL url=new URL("http://www.whatever.com/servlets/store?text=storeThisTExt");
      SiteConn aConnection=new SiteConn(url);
      StringBuffer sb=aConnection.getContens();
      System.out.println(sb.toString());
    } catch (MalFormedURLException mfe) {
      System.err.println(mfe.getMessage());
    }
     *
     */
    
    public class SiteConn {
      URL url=null;
      public SiteConn(URL  url) {
        this.url=url;
      }
      public StringBuffer getContents() throws Exception {
        StringBuffer buffer;
        String line;
        int responseCode;
        HttpURLConnection connection;
    
        BufferedReader dataInput;
        connection = (HttpURLConnection)url.openConnection();
        responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
          throw new Exception("HTTP response code: " +
             String.valueOf(responseCode));
        }
        try {
          buffer = new StringBuffer();
          InputStream input = connection.getInputStream();
          dataInput = new BufferedReader(new InputStreamReader(input));
    
          while ( (line = dataInput.readLine()) != null) {
            buffer.append(line);
            buffer.append('\n');
            //System.out.println("line: " + line);
          }
          input.close();
        } catch (Exception ex) {
          ex.printStackTrace(System.err);
          return null;
        }
       return buffer;
      }
      public byte [] getByteBuffer () throws Exception {
    
        String line;
        int responseCode;
        HttpURLConnection connection;
    
    
        connection = (HttpURLConnection)url.openConnection();
        InputStream input=connection.getInputStream();
    
        responseCode = connection.getResponseCode();
    
        if (responseCode != HttpURLConnection.HTTP_OK) {
          throw new Exception("HTTP response code: " +
             String.valueOf(responseCode));
        }
    
        try {
          byte [] b=new byte[1420];
          BufferedInputStream in=new BufferedInputStream(input);
          ByteArrayOutputStream out=new ByteArrayOutputStream();
          System.out.println("starting");
          int n=in.read(b);
          System.out.println("start:"+n);
          while (n  > 0) {
            out.write(b,0,n);
            n=in.read(b);
            System.out.println("buf:"+n);
          }
          out.close();
          input.close();
          return out.toByteArray();
        } catch (Exception ex) {
          ex.printStackTrace(System.err);
          return null;
        }
    
      }
    
    }
    Last edited by sjalle; 01-07-2006 at 11:09 AM.
    eschew obfuscation

Similar Threads

  1. Replies: 3
    Last Post: 06-08-2005, 04:22 AM
  2. Calling a Servlet from an Applet
    By Angel in forum Java
    Replies: 1
    Last Post: 03-02-2001, 09:29 AM
  3. Replies: 1
    Last Post: 08-10-2000, 12:35 PM
  4. Replies: 1
    Last Post: 08-10-2000, 12:33 PM
  5. applet servlet communication
    By prasad in forum Java
    Replies: 1
    Last Post: 05-26-2000, 11:11 AM

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