Applet Servlet Communication...

Discussions

Web tier: servlets, JSP, Web frameworks: Applet Servlet Communication...

  1. Applet Servlet Communication... (2 messages)

    Hi,
    I am trying to get a applet to communicate with my servlet. I am able to receive from the servlet. But the message I am trying to send is not reaching the servlet.

    Can someone help? Am I doing something wrong?

    I call the applet's Refresh() function.

    This is the (applet) code for the Refresh function
    -----------------
        public void Refresh()
            throws MalformedURLException, IOException
        {
           String cr = "\r\n";
           URL url = new URL(getCodeBase(), "/servlet/ExcelServlet");
           genExceptions = url.toString();
           URLConnection con = url.openConnection();
           con.setUseCaches(false);
           con.setDoInput(true );
           con.setDoOutput (true);
           con.connect();
           try
           {
                OutputStream out = con.getOutputStream();
                InputStream in = con.getInputStream();

                DataOutputStream outputStream;
                outputStream = new DataOutputStream( out );

                outputStream.writeUTF( "POST /servlet/ExcelServlet HTTP/1.0" + cr );
                outputStream.writeUTF( "Content-Length: 15" + cr + cr );
                outputStream.writeUTF( "Hi there How are you" + cr );
                
                DataInputStream textStream;
                textStream = new DataInputStream(in);
                dispString = textStream.readLine();
           }
           catch( Exception exception)
           {
               genExceptions = exception.toString();
           }
           repaint();
        }
    -----------------

    This is the servlet code snippet
    -----------
    public void doPost(HttpServletRequest request,
                          HttpServletResponse response)
        throws IOException, ServletException
    {

    System.out.println( "Get" );
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    out.println( "Myfile.xls");
    BufferedReader in = request.getReader();

    Enumeration enu = request.getHeaderNames();
    while ( enu.hasMoreElements() )
    {
        String Head = (String) enu.nextElement();
        System.out.println( Head );
    }

    System.out.println( "Excel Servlet Length : " + request.getContentLength() );

    }
    -----------
  2. Applet Servlet Communication...[ Go to top ]

    Trap your request in the doGet method of your servlet. Try this code for HTTP POST:

      DataOutputStream printout;
      BufferedReader rdr;

      String u = <your Page>;
      URL url = new URL(u);
      URLConnection urlConn = url.openConnection();
      urlConn.setDoInput(true);
      urlConn.setDoOutput(true);
       urlConn.setUseCaches(false);
       urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        printout = new DataOutputStream(urlConn.getOutputStream());
       
    String P = "P1=" + URLEncoder.encode(<val1>) + "&P2=" + URLEncoder.encode(<val2>);

                printout.writeBytes(P);
                printout.flush();
                printout.close();
                rdr = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
                retVal = "";
                while (null != ((str = rdr.readLine()))) {
                    retVal = retVal + str;
                }
                rdr.close();

  3. Check http://www.javagalaxy.com there are examples on Applet Servlet communication.

    Uma