Discussions

Web tier: servlets, JSP, Web frameworks: Applet to servlet communication

  1. Applet to servlet communication (7 messages)

    I will appreciate it if anybody can tell me where I can learn how to implement applet to servlet communication.

    Threaded Messages (7)

  2. Hunter & Crawford's <i>Java Servlet Programming</i> has some good coverage of applet-servlet communications. It covers using RMI, sockets, and straight HTTP and serialization. You might be able to find some more info at http://www.servlets.com.

    Andy
  3. You can try this piece of code in the Applet, open a HTTP Connection , override the Service method in the Servlet and return an Object...

    URL url = new URL("http://xxx.xxx.xxx.xxx:7001/servlets/../../..");
    URLConnection uc = url.openConnection();
    uc.setDoOutput(true);
    uc.setDoInput(true);
    //uc.setUseCache(false);
    uc.setRequestProperty("CONTENT_TYPE","application/octet-stream");
    os= uc.getOutputStream();
    //myclass ab = new myclass(os);
    out = new ObjectOutputStream(os);

    out.writeObject("Give the reference to the Object here");


    //receiving the object
    in = new ObjectInputStream(uc.getInputStream());
    Type reference = (Type)in.readObject();
    return reference;

    And this in the Servlet :

    public void service(HttpServletRequest req,HttpServletResponse res)
        throws ServletException,IOException
        {
    ServletInputStream servletIn;
    ServletOutputStream servletOut;
    res.setContentType("text/html");



    try {
    servletIn = req.getInputStream();
    servletOut = res.getOutputStream();

    ObjectInputStream in = new ObjectInputStream( servletIn );
    ObjectOutputStream objout = new ObjectOutputStream(servletOut );
    java.util.Locale lcountry = req.getLocale();
    System.out.println("Country is " + lcountry.getCountry());
    //Object will be read here..
    String abc = (String)in.readObject();



    objout.writeObject("send any object you want");
    servletIn.close();
        servletOut.close();

          } catch (Exception e) {
             System.out.println(e.toString());
             return;
          }


       }

    get back if you have any problems..

  4. pls help me[ Go to top ]

    hello

    I have the same problem in applet sends serial object to servlet.

    Would anybody help me ?

    thanks advance!

    fxwolf.
  5. Why dont we use webservices?[ Go to top ]

    Hi;

    Why don't we use Webservices for this kind of requirements?
    I assume , the need is to send data between an applet and a servlet? >> Why use a servlet?

    Expose the classes behind the servlet as webservices and invoke them without a servlet in between and send-recieve appropriate data..?

    I believe you want to send request to the server from an applet and get the appropriate response? with or with out a servlet?

    Do u want to print the Servlet outstream HTML directly to the Applet? or obtain data from the outstream specifically?


    Any corrections are welcome..!

    Regards;
    Jyothish
  6. hi rite now i m working on the applet servlet communication . and i m facing the same problem .. can i get the full code or can i get the correction u done on the code . it will be very helpful for me . thanks in advance can u send it in to my id joju2002 at rediffmail dot com joseph thomas
  7. Hi! All. I felt that this thread resembles my subject in concern, so I'm posting this here,excuse me for coming in with a new topic. Let me describe my scenario. I wanted to access the file system of the client machine, so I have no way but to go for Applet. And I'm duping a lot of classes , nearly 1.5 MB of classes put in a jar file, and sent to client via a Signed Applet. The problem is My client does not want a swing interface. He needs web UI. So, what I'm planning is, send the jar files to the client, and Could ant one tell me invoke the methods in classes of those jar file, by Servlet or JSP, even by means of JSObject(JavaScript object for java). How to invoke the methods in claases on client machine(Applet), by JSP or Servlet? Thanking you, Samba.
  8. Applet to 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); Sudhir http://www.jyog.com