You should use the URLConnection object in the applet, and you open a url
socket to the servlet, then in the servlet use the ServletOutputStream in
order to send your DataOutputStream that is prepared in servlet.

Look at the following code fragments...

----This is servlet side code: in this code a DataOutputStream is created
from ByteArrayOutputStream and its is sent by ServletOutputStream as byte
array----

  public void doPost(HttpServletRequest req,
                    HttpServletResponse resp)
    throws ServletException, java.io.IOException
    {

      // if you want to read data from applet, DataInputStream is created
from request's getInputStream
      // Get the input stream for reading data from the client
      DataInputStream in =
        new DataInputStream(req.getInputStream());

      // Read the some string
      String s1 = in.readUTF();
      String s2 = in.readUTF();

      // We'll be sending binary data back to the client so
      // set the content type appropriately
      resp.setContentType("application/octet-stream");

      // Data will always be written to a byte array buffer so
      // that we can tell the client the length of the data
      ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

      // Create the output stream to be used to write the
      // data to our buffer
      DataOutputStream out = new DataOutputStream(byteOut);

      out.writeUTF("some sring");
      ......  //write all parameters into DataOutputStream "out"

      // Flush the contents of the output stream to the
      // byte array
      out.flush();

      // Get the buffer that is holding our response
      byte[] buf = byteOut.toByteArray();

      // Notify the client how much data is being sent
      resp.setContentLength(buf.length);

      // Send the buffer to the client
      ServletOutputStream servletOut = resp.getOutputStream();

      // Wrap up
      servletOut.write(buf);
      servletOut.close();
    }

----Here is the applet side code: first create an URL connection to the
servlet and then write or read data from stream---

  protected void sendSomeData()
    throws Exception
  {

      // Get the server URL
      java.net.URL url = new java.net.URL(codeBase + servlet);

      // Attempt to connect to the host
      java.net.URLConnection con = url.openConnection();

      // Initialize the connection
      con.setUseCaches(false);
      con.setDoOutput(true);
      con.setDoInput(true);

      // these parts are to send data to the server, the preparing byte
array procedure is the same with servlet counterpart.
      // Data will always be written to a byte array buffer so
      // that we can tell the server the length of the data
      ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

      // Create the output stream to be used to write the
      // data to our buffer
      DataOutputStream out = new DataOutputStream(byteOut);

      // Send some string
      out.writeUTF(s1);
      out.writeUTF(s2);

      // Flush the data to the buffer
      out.flush();

      // Get our buffer to be sent
      byte buf[] = byteOut.toByteArray();

      // Set the content that we are sending
      con.setRequestProperty("Content-type",
                             "application/octet-stream");

      // Set the length of the data buffer we are sending
      con.setRequestProperty("Content-length",
                             "" + buf.length);

      // Get the output stream to the server and send our
      // data buffer
      DataOutputStream dataOut =
        new DataOutputStream(con.getOutputStream());
      dataOut.write(buf);

      // Flush the output stream and close it
      dataOut.flush();
      dataOut.close();

      // these parts are for reading data from servlet
      // Get the input stream we can use to read the response
      DataInputStream in =
        new DataInputStream(con.getInputStream());

      // Read the response from the server, the order of read must be same
the
      // order of write that was done in servlet. and the read type is also
important
        someString = in.readUTF();

      // Close the input stream
      in.close();
  }
----------------------------------------------------------------------------
------------------------

if you have difficulties with using streams, please let me know...

Deniz...

----- Original Message -----
From: "dushyanth harinath" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, October 09, 2000 5:00 PM
Subject: servlet-applet communication


> Hi folks,
> I want to invoke an applet from a servlet and send
> 10/15 parameters to it which i retreive from the
> database using a servlet.How should i go about
> acheiving this.
> Any help will be greatly appreciated
> thanks
> dushyanth
>
> =====
> H.Dushyanth
> Programmer
> Archean Infotech Limited
> http://www.archeanit.com
> Where Technology Meets Business
>
> ____________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.co.in address at http://mail.yahoo.co.in
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to