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.
LVL 1
rt_vinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lhankinsCommented:
Can you provide a stack trace...?

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

0
CEHJCommented:
>>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
0
rt_vinAuthor Commented:
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??
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

lhankinsCommented:
I think CEHJ touched on this :

You're calling con.getInputStream() twice...

>
>    InputStream inputstream = con.getInputStream();
>               
>    ObjectInputStream ois = new ObjectInputStream(con.getInputStream());

You probably don't want to do that.   Try what he suggested :

    InputStream inputstream = con.getInputStream();
               
    ObjectInputStream ois = new ObjectInputStream(inputstream);

0
CEHJCommented:
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
0
rt_vinAuthor Commented:
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.
0
rt_vinAuthor Commented:
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
0
CEHJCommented:
>>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?
0
rt_vinAuthor Commented:
Yes... that is what I mean.
0
rt_vinAuthor Commented:
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??
0
CEHJCommented:
Try some experimentation: do a binary dump of what is returned by writeObject. On the client end, experiment by reading the response with an ordinary stream. Once you've got the demarcation right, you will have the right means to ensure the ObjectInputStream is being read correctly
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.