Link to home
Start Free TrialLog in
Avatar of rt_vin
rt_vin

asked on

Serialized Objects between Java Swing and Java Servlets

I am trying to serialize a some simple strings on a servlet before streaming them to a java swing program, but i am experiencing some problems... can anyone help me?

Here is my servlet:

public class swingServlet extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException, IOException{

res.setContentType("application/octet-stream");

String name1="joe";
String name2="mo";

try
{
   ObjectOutputStream ois = new ObjectOutputStream(res.getOutputStream());
   ois.writeObject(name1);
   ois.writeObject(name2);
   ois.flush();
   ois.close();
}
catch (Exception e)
{
   e.printStackTrace();
}

}
}


And here is part my swing program:

try
{
URL host = new URL("http://localhost:8080/servlet/swingServlet?x=hello&y=world");

      HttpURLConnection con = (HttpURLConnection)host.openConnection();      
      con.setRequestMethod("POST");
                                
      InputStream inputstream = con.getInputStream();
                  
      ObjectInputStream ois = new ObjectInputStream(con.getInputStream());
                JOptionPane.showMessageDialog(this, ois.readObject());
                ois.close();            
      inputstream.close();
}
catch(IOException ioe){System.out.println(ioe.getMessage());}
catch (ClassNotFoundException ce){System.err.println(ce);}


I can send the name value pairs from the swing program to the servlet, but can not receive any variables back (these should be displayed in message dialogs for testing purposes)... i don't want to use a '.out' or xml file to store the serialized data.

The error I am getting is: "Invalid stream header".

Any help is very much appreiciated.

Thanks,

Vinny.
Avatar of lhankins
lhankins
Flag of United States of America image

Can you provide a stack trace...?

What happens if you just print out the inputstream on the client...?  does it look right...?  

Avatar of CEHJ
>>ObjectInputStream ois = new ObjectInputStream(con.getInputStream());

should be

ObjectInputStream ois = new ObjectInputStream(inputstream);

and you need to call readObject twice since you've called writeObject twice
Avatar of rt_vin
rt_vin

ASKER

lhankins - when i try to print out the inputstream on the client i just get null (blank)...

...CEHJ - Still getting "Invalid stream header".

Any ideas??
SOLUTION
Avatar of lhankins
lhankins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not entirely convinced you can do what you're trying to do, as the http protocol may interfere with what you need, but difficult to say exactly without full knowledge about what's actually getting read and written
Avatar of rt_vin

ASKER

Ok...
I'm not that advanced in java to say whether this would work or not, but when i write the serialized objects from the servlet onto an '.out' (or any text based file like '.txt') file and then get the swing to read the file it works, i.e. the objects can be retrieved... so I am assuming that it works without the file.

By the way... 'lhankins', I tried what 'CEHJ' suggested but it still gives me an error.
Avatar of rt_vin

ASKER

Maybe someone understands this better than me... but i'm sure the answers in here somewhere, i just don't understand it: http://www.javacaps.com/java_serial.html
>>but when i write the serialized objects from the servlet onto an '.out' (or any text based file like '.txt') file and then get the swing to read the file it works

Do you mean write it to file at the server instead of to the response output stream?
Avatar of rt_vin

ASKER

Yes... that is what I mean.
Avatar of rt_vin

ASKER

CEHJ... I know i've already bugged you for one day :-) but do you know why my program doesn't work with the stream thing??
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
8-)