try using this approach: it should work


           try {
              action = new URL(ss);
              url = action.openConnection();
              url.setDoInput(true);
              url.setDoOutput(true);
              url.setUseCaches(false);
              ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

url.setRequestProperty("Content-Type","application/octet-stream");
              out = new ObjectOutputStream(byteOut);
              out.writeObject(obj);
              out.flush();
              byte[] buf=byteOut.toByteArray();
              url.setRequestProperty("Content-length"," "+buf.length);
              DataOutputStream  servletOut = new
DataOutputStream(url.getOutputStream());
              servletOut.write(buf);
              servletOut.close();
           }
         catch(MalformedURLException me) {

              System.out.println("MalformedURLException");
         }
         catch (IOException ee) {
            System.out.println("IOException");
          }

            try {
                  in=new ObjectInputStream(url.getInputStream());
                  ht = (Hashtable)in.readObject();
                  in.close();
               }
           }
           catch (Exception e) {
           }

----- Original Message -----
From: Manavalan, EzhilX <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, August 13, 1999 11:59 AM
Subject: Servlet from an applet


> Hi,
> I want to invoke a servlet from an applet.I'll do some modifications(say
> increment a variable) in the servlet and send it back to
> applet for display.I'm getting IOException.Can anyone help me out.
> Thanks in advance.
>
> public class STrack implements Serializable
> {
> String count = new String(""); String str = new String(""); public String
> getCount()
> { return count; }
> public void setCount(String c)
> { count = c; }
> public void setOrder(String ss)
> { str = ss; }
> public String getOrder()
> { return str; }
> }
> public class CountServletApplet1 extends java.applet.Applet
> {
> private TextField SessionField,tf;
> private Button SessionCount;
> private Button GlobalCount;
> private Button FileCount;
> boolean isStandalone = false;
>
>
> private URLConnection connect;
> private URL u1;
>
> String sum = "0";
> public CountServletApplet1()
> {
> setLayout(new FlowLayout());
> add(new Label("Count"));
> SessionCount = new Button("Session Count");
> FileCount = new Button("File Count");
> GlobalCount = new Button("GlobalCount");
> SessionField = new TextField("1");
> tf = new TextField("");
> add(SessionCount);
> add(GlobalCount);
> add(FileCount);
> add(SessionField);
> add(tf);
> setVisible(true);
> }
> public static void main(String str[])
> {
> Frame frm = new Frame();
> CountServletApplet app = new CountServletApplet();
> frm.add(app); app.init(); app.start();
> frm.show();
> }
> public String getParameter(String key,String def)
> {
> showStatus("I'm in parameter");
> return isStandalone?System.getProperty(key,def):
> (getParameter(key)!=null?getParameter(key):def);
> }
> public STrack readTrack(URLConnection con)
> {
> STrack track = null;
> try
> {
> ObjectInputStream is = new ObjectInputStream(con.getInputStream());
> System.err.println("Waiting for the response now");
> track = (STrack)is.readObject();
> is.close();
> }
> catch(Throwable x){x.printStackTrace();}
> return track;
> }
> public void writeTrack(URLConnection con,STrack tt)
> {
> try
> {
> con.setUseCaches(false);
> con.setRequestProperty("CONTENT_TYPE","application/octet-stream");
> con.setDoInput(true);
> con.setDoOutput(true);
> ObjectOutputStream os = new ObjectOutputStream(con.getOutputStream());
> System.err.println("Writing sessiontrack Object");
> os.writeObject(tt);
> os.flush();
> os.close();
> }
> catch(Throwable x){x.printStackTrace();}
> }
> public boolean action(Event ae, Object arg)
> { if(ae.target == SessionCount)
> {
> try
> {
> showStatus("Clicked the local session track button");
> System.out.println("Resetting Count..");
> STrack track = new STrack();
> track.setOrder(tf.getText());
> u1 = new URL("http://localhost:8080" + "/servlet/CountServlet1");
> System.err.println("Resetting Count..");
> URLConnection con = u1.openConnection();
> writeTrack(con,track);
> STrack tt = readTrack(con);
> if(tt!=null)
> {
> showStatus("U r in text field");
> SessionField.setText(tt.getCount());
> }
> else
> {
> showStatus("U r in text field");
> System.err.println("ReadObject failed");
> return false;
> }
>
> }
> catch(Throwable x){x.printStackTrace();return false;}
> return true;
> }
> }
> public class CountServlet1 extends HttpServlet
> { int count;
> public void init(ServletConfig conf) throws ServletException
> {
> super.init(conf); count = 1;
> }
> public void service(HttpServletRequest req, HttpServletResponse res)
> {
> try
> {
> ObjectInputStream ois = new ObjectInputStream(req.getInputStream());
> STrack track = (STrack)ois.readObject();
> getTrackStatus(track);
> res.setContentType("application/octet.stream");
> ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
> oos.writeObject(track);
> oos.flush();
> oos.close();
> }
> catch(Throwable e)
> { e.printStackTrace(); }
> }
> private void getTrackStatus(STrack track)
> { Integer c = new Integer(count + 1);
> String str = c.toString();
> track.setCount(str);
> }
> }
>
>
___________________________________________________________________________
> 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