part 2:
My applet aTos.java
public class aTos extends java.applet.Applet implements ActionListener {
dataTransfer dts,dts1;
String servletPath="http://192.168.0.10/examples/servlet/sToa";
String errormessage="test",error1="",error2="";
Mycanvas drawarea;
boolean draw=false;
int dtsyear=0,dtsmonth=0;
int x0=30,y0=350;
/*******deal with draw button input**************************************************/
public void actionPerformed(ActionEvent buttonevt) {
if(buttonevt.getSource()==drawbutton){
dts=new dataTransfer(2000,2000);
/***send object to servlet***/
try{
URL url=new URL(servletPath);
URLConnection conn=url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Content-Type","application/octet-stream");
ObjectOutputStream toServlet=new ObjectOutputStream(conn.getOutputStream());
toServlet.writeObject(dts);
toServlet.flush();
toServlet.close();
}catch(Exception e){error1=e.toString();}
/***get object from servlet***/
try{
URL url=new URL(servletPath);
URLConnection conn=url.openConnection();
/***Applet client-side get a object from servlet***/
conn.setDoOutput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Content-Type","application/octet-stream");
/***read the input from servlet***/
ObjectInputStream fromServlet=new ObjectInputStream(conn.getInputStream());
dts1=(dataTransfer)fromServlet.readObject();
dtsyear=dts1.dyear;
dtsmonth=dts1.dmonth;
}catch(Exception e){error2=e.toString();}
draw=true;
drawarea.repaint();
}
}//end of draw button action
public class Mycanvas extends java.awt.Canvas{
public void paint(Graphics g){
if(draw){
g.setColor(Color.black);
g.drawString("draw: true ",x0+85,y0-230);
g.drawString("error1"+error1,x0+85,y0-210);
g.drawString("error2"+error2,x0+85,y0-190);
g.drawString("dyear:"+Integer.toString(dtsyear),x0+85,y0-170);
g.drawString("dmonth: "+Integer.toString(dtsmonth),x0+85,y0-150);
draw=false;
}
else{
g.setColor(Color.black);
g.drawString("draw: false ",x0+85,y0-130);
g.drawString("error1"+error1,x0+85,y0-110);
g.drawString("error2"+error2,x0+85,y0-90);
g.drawString("dyear:"+Integer.toString(dtsyear),x0+85,y0-70);
g.drawString("dmonth: "+Integer.toString(dtsmonth),x0+85,y0-50);
}
}//end of paint
}//end of Mycanvas calss
}//end of applet
My dataTransfer.class object:
import...
public class dataTransfer implements Serializable{
int dyear,dmonth;
dataTransfer(int arg1,int arg2){
dyear=arg1;
dmonth=arg2;
}
}
My servlet: sToa.java
import..
public class sToa extends HttpServlet {
dataTransfer dts2,dts3;
int dts2year,dts2month;
String error3;
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
ObjectInputStream fromApplet=null;
ObjectOutputStream toApplet;
PrintWriter out=null;
try{
/**get input stream from applet**/
fromApplet=new ObjectInputStream(request.getInputStream());
/**read the serialized object from applet**/
dts2=(dataTransfer) fromApplet.readObject();
fromApplet.close();
dts2year=dts2.dyear;
dts2month=dts2.dmonth;
if(dts2month==2000){
dts3=new dataTransfer(3000,3000);
}
else{
dts3=new dataTransfer(4000,4000);
}
/**servlet side code to send a serialized object to applet**/
response.setContentType("java-internal/"+dataTransfer.class.getName());
toApplet=new ObjectOutputStream(response.getOutputStream());
toApplet.writeObject(dts3);
toApplet.flush();
toApplet.close();
}catch(Exception e) {error3=e.toString();}
}//end of servlet