wood burning stoves 2.0 *
The moose likes Servlets and the fly likes how to send parameters form applet to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to send parameters form applet to servlet" Watch "how to send parameters form applet to servlet" New topic
Author

how to send parameters form applet to servlet

anirudh takkallapally
Greenhorn

Joined: Sep 04, 2003
Posts: 14
hi,
the problem is i am not able to debug my code , well for testing purpose i am trying to send an objet to the servlet make some manipulations to it and trying to get it back , but when i click on the submit button on my applet nothing is happening , well my applet is on also on the server.. , please look into this code and tell me if u can find any obvious mistakes....
this is my applet code
public class PipelineApplet3 extends Applet implements ActionListener
{
private static final String servlet="http://localhost:8080/examples/servlet/PipelineServlet3";
private Label text ;
String msg;
public class TestVar implements Serializable
{
private void writeObject(java.io.ObjectOutputStream out){}
//throws IOException;
private void readObject(java.io.ObjectInputStream in){}
//throws IOException;
int x,y;
String mesg;
};
TestVar ob= new TestVar();
private URLConnection getConnection() throws IOException
{
URL u =new URL( servlet );
URLConnection con = u.openConnection();
con.setDoInput( true );
con.setDoOutput( true );
con.setUseCaches( false );
con.setAllowUserInteraction( false );
return con ;
}
private void sendObject( URLConnection con , Object obj ) throws
IOException
{
ObjectOutputStream out = new ObjectOutputStream(
con.getOutputStream() );
if ( obj != null )
{
out.writeObject(obj);
}
out.close();
}
private Object receiveObject( URLConnection con ) throws Exception
{
ObjectInputStream in = new ObjectInputStream( con.getInputStream() );
Object obj = in.readObject();
in.close();
return obj ;
}
public void actionPerformed( ActionEvent e )
{
try
{
URLConnection con = getConnection();
sendObject( con , ob );
TestVar s = (TestVar)receiveObject( con );
msg=s.mesg;
repaint();
}catch( Exception ex){
System.out.println("Curses ! Foiled again: " + ex );
}
}
public void init()
{
ob.x=10;
ob.y=20;
ob.mesg="anirudh";
Button b = new Button("Test!");
b.addActionListener(this);
add(b);
//text = new Label(" the world is become a big ");
//add(text);
}

public void paint (Graphics g) {
g.drawString(msg,200,120);
}
}
and this is my servlet code,
public class PipelineServlet3 extends HttpServlet {
public class TestVar implements Serializable
{
private void writeObject(java.io.ObjectOutputStream out){}
//throws IOException;
private void readObject(java.io.ObjectInputStream in){}
//throws IOException;
int x,y;
String mesg;
};
private Object receiveObject( HttpServletRequest req ) throws Exception
{
ObjectInputStream in = new ObjectInputStream( req.getInputStream() );
Object obj = in.readObject();
in.close();
return obj ;
}
private void sendObject( HttpServletResponse resp ,Object obj )
throws Exception
{
ObjectOutputStream out = new ObjectOutputStream( resp.getOutputStream() );
out.writeObject( obj );
out.close();
}
public void doPost( HttpServletRequest req ,HttpServletResponse resp )
{
try {
Object obj = receiveObject( req );
TestVar ob=(TestVar)obj;
ob.x=ob.x+10;
ob.y=ob.y+20;
ob.mesg="BOB";
sendObject( resp , ob );
}catch ( Exception e ) {
System.out.println( " Clang! thunk: " + e );
}
}
}
Joshua Doerring
Greenhorn

Joined: Aug 08, 2003
Posts: 19
There's a couple of things I noticed.
In the Applet's getConnection() method put
con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
also, in the sendObject() method put out.flush() after you write the object.
you should also do the same in the servlet.
response.setContentType("application/x-java-serialized-object");
and out.flush() after you write the object.
Hope this helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to send parameters form applet to servlet