?
Solved

Serialized Objects between Java Swing and Java Servlets

Posted on 2004-12-01
12
Medium Priority
?
262 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:rt_vin
  • 5
  • 5
  • 2
12 Comments
 
LVL 7

Expert Comment

by:lhankins
ID: 12720088
Can you provide a stack trace...?

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12721196
>>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
 
LVL 1

Author Comment

by:rt_vin
ID: 12721351
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:lhankins
lhankins earned 225 total points
ID: 12721410
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12721418
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
 
LVL 1

Author Comment

by:rt_vin
ID: 12724342
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
 
LVL 1

Author Comment

by:rt_vin
ID: 12724420
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12728678
>>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
 
LVL 1

Author Comment

by:rt_vin
ID: 12729046
Yes... that is what I mean.
0
 
LVL 1

Author Comment

by:rt_vin
ID: 12750073
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 225 total points
ID: 12752602
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12812976
8-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question