File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass object btw applet and servelt Part1

 
may Lee
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to pass a java object passed between Applet and Servlet.My test is: when use visit my website //192.168.0.10/test.html,and click the draw button on the applet, the applet send the object with some parameters to servlet,then servlet passes back the object with changed parameters

Applet is a front interface put at Apache web server:/var/www/html/. Servelt is supported on Tomcat4.1.30,it's sotred at /webapps/examples/WEB-INF/classes.The object passed between Applet and Servelt is stored both as /var/www/html/ and /webapps/examples/WEB-INF/classes, two copies.

List of files:
/var/www/html/test.html
/var/www/html/aTos.class(my applet)
/var/www/html/dataTransfer.class(object transfer between applet ans servlet)
/webapps/examples/WEB-INF/classes/sToa.class(my servlet)
/webapps/examples/WEB-INF/classes/dataTransfer.class(same as the above one)

I have add the servlet-map to web.xml
<servlet>
<servlet-name>sToa</servlet-name>
<servlet-class>sToa</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sToa</servlet-name>
<url-pattern>/sToa</url-pattern>

Then I tested and got the error:java.io.IOException Server returned HTTP response code 4.Could any expert have a look of my question? I am not sure the setContentType in my servlet. Can it be done in doPOST?
Nest post part2 is my source code of applet, servlet,and object. I only give the main part.Sorry, it is so long question! But the source code is quite simple, cos it is just for test purpose.
</servlet-mapping>
 
may Lee
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
may Lee
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
more exact, the error I got is not error1, it is error2, so it is when servlet transfer object to applet? or :
error2: java.io.IOException Server returned HTTP response code: 4
 
may Lee
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found 2 errors:

First, in my second try{} in my applet, I made a mistake, it should be
conn.setInput(true)
conn.setInput(true)
NOT
conn.setOutput(true)
conn.setOutput(true)

Second,it looks like I cannot use twice try{}catch{} to connect to servlet
so I combine the two to one as the followings, then it is working. I am happy it is working, but don't know why I cannot do twice URL connection? Mysterious to me!!

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");
/***send object to servlet***/
ObjectOutputStream toServlet=new ObjectOutputStream(conn.getOutputStream());
toServlet.writeObject(dts);
toServlet.flush();
toServlet.close();

ObjectInputStream fromServlet=new ObjectInputStream(conn.getInputStream());
dataTransfer dts1=(dataTransfer)fromServlet.readObject();
dtsyear=dts1.dyear;
dtsmonth=dts1.dmonth;
}catch(Exception e){error1=e.toString();}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic