Hi,
I'm currently doing a  servlet/applet application.Th eservlet is invoked
from an applet.
The servlet does some calculations and returns to the applet.In the
textarea,the
output is displayed.I'm getting IOException.
can anyone help me out.

I'm sending my code also.

Thanks in advance.

Tmmet.

//Applet code
import java.awt.*;
import java.io.*;
import java.net.*;
import java.io.*;


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);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
frm.setSize(size);
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();}
//catch(IOException e){System.out.println(" Excep in
readTrack");System.err.println(e.getMessage());}
//catch(ClassNotFoundException ex){System.out.println(" Excep in
readTrack");System.err.println(ex.getMessage());}
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();}
// catch(IOException e){System.out.println(" Excep in writeTrack"+
tt.toString() );System.err.println(e.getMessage());}
}
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;}
//catch(MalformedURLException e){System.out.println("Malformed Excep in
action");System.err.println(e.getMessage());return false;}
// catch(Exception ex){ System.out.println("Malformed Excep1 in
action");System.err.println(ex.getMessage());return false;}

return true;

}
}

//Servlet code
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

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);
}
}

//Serialization class file

import java.io.*;

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;
}
}

___________________________________________________________________________
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