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("applic ation/octe t-stream") ;
String name1="joe";
String name2="mo";
try
{
ObjectOutputStream ois = new ObjectOutputStream(res.get OutputStre am());
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.op enConnecti on();
con.setRequestMethod("POST ");
InputStream inputstream = con.getInputStream();
ObjectInputStream ois = new ObjectInputStream(con.getI nputStream ());
JOptionPane.showMessageDia log(this, ois.readObject());
ois.close();
inputstream.close();
}
catch(IOException ioe){System.out.println(io e.getMessa ge());}
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.
Here is my servlet:
public class swingServlet extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
res.setContentType("applic
String name1="joe";
String name2="mo";
try
{
ObjectOutputStream ois = new ObjectOutputStream(res.get
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.op
con.setRequestMethod("POST
InputStream inputstream = con.getInputStream();
ObjectInputStream ois = new ObjectInputStream(con.getI
JOptionPane.showMessageDia
ois.close();
inputstream.close();
}
catch(IOException ioe){System.out.println(io
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.
>>ObjectInputStream ois = new ObjectInputStream(con.getI nputStream ());
should be
ObjectInputStream ois = new ObjectInputStream(inputstr eam);
and you need to call readObject twice since you've called writeObject twice
should be
ObjectInputStream ois = new ObjectInputStream(inputstr
and you need to call readObject twice since you've called writeObject twice
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??
...CEHJ - Still getting "Invalid stream header".
Any ideas??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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.
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?
Do you mean write it to file at the server instead of to the response output stream?
ASKER
Yes... that is what I mean.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
8-)
What happens if you just print out the inputstream on the client...? does it look right...?