aspose file tools*
The moose likes Servlets and the fly likes passing parameters form applet to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing parameters form applet to servlet" Watch "passing parameters form applet to servlet" New topic
Author

passing parameters form applet to servlet

anirudh takkallapally
Greenhorn

Joined: Sep 04, 2003
Posts: 14
hi ,
i need to send 4 ints 2 strings to a servlet from an applet , the servlet invokes a process with this information and generates a graph , i have to display this graph .
my main question is how to send the information to the servlet , i have tried to serialize the object like this
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;
};
and using the post method to send this object but it dint work.
Please tell me if this is right , and if u can provide me with a working code for passing the parameters form applet to servlet ,i would appriciate it.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Passing a set of parameters like that as a serialized object is a great way to do it. You say:
but it dint work.

You really need to work on your error reporting!
What happened exactly?
Bill
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 );
}
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: passing parameters form applet to servlet