It's not a secret anymore!
The moose likes I/O and Streams and the fly likes very urgent...plz help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "very urgent...plz help" Watch "very urgent...plz help" New topic

very urgent...plz help

rajarshi dutta

Joined: Jul 03, 2006
Posts: 4
My java application is communicating with a servlet or jsp,I am writing an object in server side(jsp) and trying to read it back from a java application.

server-side code

HashMap map = new HashMap();

OutputStream os = response.getOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(os);


java application code...

URL newURL = new URL("http://localhost:7001/Object.jsp");
HttpURLConnection urlConnection = (HttpURLConnection)newURL.openConnection();
if(urlConnection!= null)
urlConnection.setUseCaches (false);
urlConnection.setDefaultUseCaches (false);

catch(FileNotFoundException fne){}
catch(IOException ex)
catch(Exception ioe){}
InputStream is = urlConnection.getInputStream();
System.out.println("After reading Input Stream1");
ObjectInputStream inputFromServlet = new ObjectInputStream(is);//getting exception here.
System.out.println("After creating ObjectOutput Stream");
HashMap map = (HashMap)inputFromServlet.readObject();

The above code is running perfectly fine when I am executing it on tomcat,oc4j or websphere but throwing exception at the time of executiing on weblogic 8.1 sp4.Exception is generated on the following line

ObjectInputStream inputFromServlet = new ObjectInputStream(is);

Here is the detail of the exception invalid stream header
at Source)
at<init>(Unknown Source)

Kindly help as early as possible.

Thanks in advance
Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
I'm not sure why it would work in some app servers and not in others. What I would suggest is that you use a servlets rather than a JSP for writing the object response to the stream. I say that because A JSP is designed for rendering text to a client while allowing you to intermingle Java code in the middle to generate dynamic content. It can easily cause problems with an app as you have coded. If, for example, some text is placed inadvertantly at the top of the file your object output stream would become corrupt as the text would be mixed with the stream in teh response. A servlet would give you much tighter control over what is sent to the client. Also, your server side JSP code is not setting the content type for the response. I'm not sure but I wouldn't be surprised if the content type is defaulted to some text variation such as text/ascii, text/html or something like that. I bet if you use a servlet and set the content type it would solve your problem as well as many future issues.

Holla at me...<br /><a href="" target="_blank" rel="nofollow"></a>
rajarshi dutta

Joined: Jul 03, 2006
Posts: 4
Thanks craig.......
I tried with the following code on the server-side for seting content-type
but in vain.It doesnot make any change at all.

String contentType = "application/x-java-serialized-object";

I am trying it with a servlet and will let u know if something good comes out.

John Calabasas

Joined: Jun 11, 2006
Posts: 17
See this thread on same issue...


Sun documentation

I hate quotations. Tell me what you know.
rajarshi dutta

Joined: Jul 03, 2006
Posts: 4
Thanks to u both and sorry for being late in reply.I have put the code in servlet as suggested by craig and things are now working perfectly fine.

Thanks once again
I agree. Here's the link:
subject: very urgent...plz help
jQuery in Action, 3rd edition