CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 1999
    Posts
    7

    Applet-Servlet Communication



    Hye Folks,


    Well, I tried applet-servlet communication but it just didn't seem to work correctly. I am sending serialized data from the applet to the servlet. The send operation works fine cos' the applet does send data. The applet is able to connect to the sevlet and the init() method of the servlet does work too. The problem is, I don't get anything back from the servlet even though I am just sending a simple string back to the applet.

    I do not want to use sockets for this problem even though I know that this would work because I do not want to complicate the entire approach.

    Basically, I want a replacement for HTML by applets as my front-end.


    Any suggestions, fellow java freaks?


    Long Live Java....


    Davender aka JaVaFLow




  2. #2
    Join Date
    Apr 1999
    Posts
    2

    need your help


    namaste ,

    i saw your message in codeguru.com posted a month ago on applet servlet communication.
    you had said that your program works well with applet to servlet(. by passing a serialised object).
    it doesn't work with servlet- applet.
    i have the reverse problem ie my program works well with servlet to applet but doesn't work
    with applet to servlet communication.
    i have sent my code with this.. if you had solved the problem. please share it with me.
    i would like to see you code



    *------ applet code ----*/

    import java.io.*;
    import java.util.Date;
    import java.applet.*;
    import java.awt.*;
    import java.net.URL;
    import java.net.URLConnection;


    public class DateApplet2 extends Applet
    {
    URL DateServletURL = null;
    URLConnection DateServletConnection = null;
    public void init()
    {
    try{

    // establishing an URL connection with servlet
    DateServletURL = new URL("http://thiru2:8080/servlet/DateServlet2");
    DateServletConnection = DateServletURL.openConnection();

    // inform connection that input and output will take place
    DateServletConnection.setDoInput(true);
    DateServletConnection.setDoOutput(true);

    // specify not to use cache
    DateServletConnection.setUseCaches(false);
    DateServletConnection.setDefaultUseCaches(false);

    // specify content type
    DateServletConnection.setRequestProperty("Content-Type","application/octet-stream");

    }

    catch(Exception e)
    {
    e.printStackTrace();
    }
    }

    public void paint(Graphics g)
    {
    try{

    // create an object output stream
    g.drawString("inside paint" ,25,25);

    ObjectInputStream InputFromServlet = new ObjectInputStream(DateServletConnection.getInputStream());
    g.drawString("inputstream created ",50,50);

    // ObjectOutputStream OutputToServlet = new ObjectOutputStream(DateServletConnection.getOutputStream());
    // g.drawString("outputstream created ",60,60);

    String str = new String();
    str = (String)InputFromServlet.readObject();

    g.drawString(" String sent from server " + str,50,100);

    InputFromServlet.close();
    }
    catch( Exception e)
    {
    e.printStackTrace();
    }
    }

    }

    /* java String servlet */

    import java.io.*;
    import java.util.Date;
    import java.util.Hashtable;

    import javax.servlet.*;
    import javax.servlet.http.*;

    public class DateServlet2 extends HttpServlet
    {
    public void init()
    {
    System.out.println("---- dateservlet started ----");
    }

    public void service(HttpServletRequest request,
    HttpServletResponse response) throws ServletException , IOException
    {

    ObjectOutputStream outputToApplet = null;
    Date DateToday = null;

    try
    {
    //inputFromApplet = new ObjectInputStream(request.getInputStream());
    //System.out.println("After new OIS");

    outputToApplet = new ObjectOutputStream(response.getOutputStream());

    System.out.println("After new OoS");

    String str = new String("---- this message is from servlet----");

    outputToApplet.writeObject(str);

    outputToApplet.flush();
    outputToApplet.close();

    System.out.println("After write");
    System.out.println("----message sent is : ---" + str);

    //inputFromApplet.close();

    }
    catch( Exception e)
    {
    e. printStackTrace();
    }

    }



    }






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured