Hi,
I have worked on ur problem and have found a solution. Pl see the commented
lines // for explanation.

1. portion of the Applet code :

This I called in response to a button click in the Applet. But u may be
calling this code on some other event.

try
{
      //testServlet is the servlet I am invoking from the applet
      //IP address 192.50.200.55 is where all my test servers are running, ur
      //machine address will be different

      URL myUrl = new URL ("http://192.50.200.55:8080/servlet/testServlet");
      URLConnection con = myUrl.openConnection();

      con.setDoInput(true);
      con.setDoOutput(true);
      con.setUseCaches(false);
      con.setDefaultUseCaches(false);
      con.setRequestProperty("Content-type", "application/octet-stream");

      ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
      SBSRequest myObject = new SBSRequest(100);

      //passing a serialized object to the servlet

      out.writeObject(myObject);
      out.close();

      //the servlet recieves the serialized object , and writes its state
      //along with HTML to a file fil.html. Applet is showing this file
      //using showDocument function. The location http://192.50.200.55/ddir
      //is mapped to the Java Web Server root directory where .html file is
      //getting created by default. u can create .html file in some other
directory                                                      //also and
map it accordingly. The location http://192.50.200.55/ddir
      //is identified by MS PWS in my case. u can also use JWS for this
purpose
      //also.

      getAppletContext().showDocument(new
URL("http://192.50.200.55/ddir/fil.html"));
}
catch(MalformedURLException ex)
{
     ex.printStackTrace();
}
catch(IOException ex)
{
     ex.printStackTrace();
}

2. portion of the Servlet code :

public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
try
{
      ObjectInputStream ins = new ObjectInputStream
(request.getInputStream());
      ObjectOutputStream outs = new
ObjectOutputStream(response.getOutputStream());

      SBSRequest sbsRequest = (SBSRequest)ins.readObject();
      int data = sbsRequest.getData();
      ins.close();

      //servlet recieves the serialized object and creates a .html file for
      //applet to show on the browser

      FileWriter out = new FileWriter("fil.html");
      out.write("<html>");
      out.write("<head><title>testServlet</title></head>");
      out.write("<body>");
      out.write("<h2>"+data+"</h2>");
      out.write("</body></html>");
      out.close();
}
catch(ClassNotFoundException ex)
{
      ex.printStackTrace();
}

} //end of doPost


3. Code for SBSRequest class(u may be using a different class definition) :

import java.io.*;

public class SBSRequest implements Serializable
{
  int i;

  SBSRequest(int ii)
  {
    i = ii;
  }

  public int getData()
  {
    return i;
  }

}

Since ur doing POST from an Applet(using URLConnection), u cannot recieve
HTML sent from the servlet by default on the browser { The HTTP response
from the server is inside the applet in a Input stream and hence not
available to the browser }. Therefore I have used an intermediate .html
file to show HTML on the browser.

Hope my soln solves ur problem.

-mukul

At 07:19 PM 8/11/99 +0100, you wrote:
>        Hi all,
>
>I have a doubt and I think the answer is very simple..
>I want to call a servlet from an applet with the method post in order to
>send an object to the servlet, and I want the servlet responds whith an HTML
>page. This page will show in the browser where the applet is..
>
>the source maybe like this :
>
>In the applet :
>
>    ....
>    URL myUrl = new URL ("http://myserver:8080/servlet/myservlet");
>    URLConnection con = myUrl.openConnection ();
>
>    con.setDoInput (true);
>    con.setDoOutput (true);
>    con.setUseCaches (false);
>    con.setDefaultUseCaches (false);
>    con.setRequestProperty ("Content-type", "application/octet-stream");
>
>    ObjectOutputStream out = new ObjectOutputStream (con.getOutputStream
>());
>    out.writeObject (myObject);
>    out.close ();
>    ....
>
>In the servlet :
>
>    public void doPost (HttpServletRequest request, HttpServletResponse
>response) {
>
>    ObjectInputStream in = new ObjectInputStream (request.getInputStream
>());
>    ObjectOutputStream out = new ObjectOutputStream
>(response.getOutputStream ());
>
>    SBSRequest sbsRequest = (SBSRequest)in.readObject ();
>    in.close ();
>
>    response.setContentType ("text/html");
>    PrintWriter out = response.getWriter ();
>    out.println ("<html>");
>    out. ....
>    out.println ("</html>");
>    out.close ();
>
>    }
>
>I tried this but the browser does not show the html page. Someone has done
>this before ?
>Thanks in advance,
>
>Juan Jose Martin

___________________________________________________________________________
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