Hello all,

I try to write a applet to invoke a servlet, but I failed, I don't
know where I make mistake, here is my source code, feom message box,it
shows IOException-2, please tell me my mistakes.
Thanks in advance.

*********************************************************
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class My2Servlet extends HttpServlet
{

        public void service (HttpServletRequest request,
                                                 HttpServletResponse response)
                throws ServletException, IOException
        {
                        try
                        {
                                ObjectInputStream in=new 
ObjectInputStream(request.getInputStream());
                                ObjectOutputStream out=new 
ObjectOutputStream(response.getOutputStream());
                                String data=(String)in.readObject();
                                in.close();
                                if (data==null)
                                {
                                        System.out.println("data not trans into 
servlet");
                                        out.writeObject(new String("error"));
                                }
                                else
                                {
                                        System.out.println("data trans to servlet OK");
                                        out.writeObject(data);
                                }
                                out.close();
                        }catch (IOException e)
                        {
                                System.out.println("server side IOException");
                        }catch (ClassNotFoundException ce)
                        {
                                System.out.println("convert string error");
                        }

        }
}

***********************************************************************
applet's method:
***********************************************************************
void button1_ActionPerformed(java.awt.event.ActionEvent event)
        {
                // to do: code goes here.
                String data=null;
                String message=new String("new message");
                InetAddress localHost=null;
                String localHostAddress=null;
                try
                {
                        localHost=InetAddress.getLocalHost();
                        localHostAddress=localHost.getHostAddress();
                } catch (UnknownHostException e)
                        {
                                System.out.println("local host not found");
                        }
                URL url=null;
                URLConnection con=null;
            try
            {
            url=new URL("http://"+localHostAddress+"/servlet/My2Servlet");
            con=url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            ObjectOutputStream out=new ObjectOutputStream(con.getOutputStream());
            out.writeObject(message);
            out.close();
            }catch (MalformedURLException me)
            {
                System.out.println("MalformedURLException");
            }catch  (IOException e)
            {
                System.out.println("IOException");
            }
        try{
            ObjectInputStream in=new ObjectInputStream(con.getInputStream());
            data=(String)in.readObject();
            in.close();
            }catch (MalformedURLException me)
            {
                System.out.println("MalformedURLException-2");
            }catch (IOException e)
            {
                System.out.println("IOException-2");
            }
            catch (ClassNotFoundException ce)
            {
                System.out.println("convert string error");
            }
        button1.setLabel(data);
    }
*******************************************************

Best regards,
 DragonTown                          mailto:[EMAIL PROTECTED]

___________________________________________________________________________
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