Applet Servlet communication


Web tier: servlets, JSP, Web frameworks: Applet Servlet communication

  1. Applet Servlet communication (4 messages)

    pls tell me how to send data from an applet to a giving my code below but its not working.
    thnks in advance.

    Aplet code:

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import java.util.*;
    public class apptoserv extends Applet implements ActionListener
    TextField tf;
    Button b;
    public void init()
    tf=new TextField(20);
    b=new Button("send");
    public void actionPerformed(ActionEvent e)
    String s=tf.getText();

    URL hp=new URL("http://localhost:8080/servlet/servtoapp");
    URLConnection hpcon=hp.openConnection();
    System.out.println("Connection estd");
    hpcon.setRequestProperty("Content-Type", "application/octet-stream");
    ObjectOutputStream os=new ObjectOutputStream(hpcon.getOutputStream());
    System.out.println("data sent");
    }catch(Exception e1){e1.printStackTrace();}

    Servlet code:

    import javax.servlet.*;
    import javax.servlet.http.*;
    public class servtoapp extends HttpServlet
    String s="";
    public void init() throws ServletException

    public void doGet(HttpServletRequest req,HttpServletResponse res)
    ObjectInputStream os=new ObjectInputStream(req.getInputStream());
    }catch(Exception e){}

    From Anindya

    Threaded Messages (4)

  2. If you are passing large chunk of data from Applet to Servlet or from Servlet to Applet NEVER use Object stream instead WRAP the Object Stream around Buffered stream. This will reduce the number of Writes to the stream and significantly improve the performance. If you sending very few objects then you are OKAY with this Approach.

    1. I am not sure why you are using Content Type of Application/octet..., Instead use "java-internal/<class name of the Object you are passing". So if you change this then you should be OKAY.

    2. Here is the code to send large chunk of data,

    InputStream writeMessage(String input) throws IOException
       ObjectOutputStream otStream;
       URL url;
       Vector newVect;
       String urlInp = "" + input;
       url = new URL(urlInp);
       con2 = url.openConnection();
       // assign newVect variable
       con2.setRequestProperty("Content-Type","java-internal/" + newVect.getClass().getName());
      BufferedOutputStream bout = new BufferedOutputStream(con2.getOutputStream(), 1024);
      ObjectOutputStream out = new ObjectOutputStream(bout);
    on the Servlet side you need to use a combination of Buffered / Object stream as used above to read the data.

    Cognizant Tech Soln
    hariprasad at mailcity dot com

  3. Applet Servlet communication[ Go to top ]

    When I was doing this with WebLogic, I ran into similar problems. What I found was that the applet didn't actually send the request until I grabbed the input stream from the url connection.

    Try adding hpcon.getInputStream() after you close your output stream in the applet. Also, you may want to have your servlet send back a simple response. I think if you just set the content type on the response, it'll at least send back and HTTP response with no body. So also add res.setContentType("text/html") to your servlet's doGet method.

    Hope this helps,

    Andy Nguyen
  4. Applet Servlet communication[ Go to top ]

      You are so damn correct.
    Thanks man.
    - a different Hari
  5. Re: Applet Servlet communication[ Go to top ]

    Here is the code which you can use to communication between applet and servlet/JSP following code can be used at applet side : /* *URL to servlet */ URL serverURL = new URL(servletPath); URLConnection connection = serverURL.openConnection(); /* * Connection will be used for both input and output */ connection.setDoInput(true); connection.setDoOutput(true); /* * Disable caching */ connection.setUseCaches(false); /* *connection will be used for transfaring serialized java objects. */ connection.setRequestProperty("Content-Type", "application/octet-stream"); ObjectOutputStream outputStream = new ObjectOutputStream(connection.getOutputStream()); outputStream.writeObject(request); outputStream.flush(); outputStream.close(); ObjectInputStream inputStream = new ObjectInputStream(connection.getInputStream()); response = (Map)inputStream.readObject(); inputStream.close(); Here response is the response Map returned from servlet. following code can be used in your servlet to send serializable objects to applet. write this code in doXXX method of servlet. ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream()); Map responseMap = new HashMap(); /* put watever objects you wants to return to applet. This objects must be serializable. responseMap.put("a", "a"); */ out.writeObject(responseMap); out.flush(); out.close(); response.setStatus(HttpServletResponse.SC_NO_CONTENT); SS