wood burning stoves*
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
Author

very urgent...plz help

rajarshi dutta
Greenhorn

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();
map.put("Babu","40");
map.put("Bishu","38");
%>

<%
OutputStream os = response.getOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(map);

oos.flush();
oos.close();
os.close();
%>

java application code...

try
{
URL newURL = new URL("http://localhost:7001/Object.jsp");
HttpURLConnection urlConnection = (HttpURLConnection)newURL.openConnection();
if(urlConnection!= null)
{
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches (false);
urlConnection.setDefaultUseCaches (false);
urlConnection.setRequestProperty("CONTENT_TYPE","application/x-java-serialized-object");
}

}
catch(FileNotFoundException fne){}
catch(IOException ex)
{
ex.printStackTrace();
System.out.println(ex.getCause());
}
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

java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<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="http://codeforfun.wordpress.com" target="_blank" rel="nofollow">http://codeforfun.wordpress.com</a>
rajarshi dutta
Greenhorn

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";
response.setContentType(contentType);

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

Thanks...........
John Calabasas
Greenhorn

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

Link

Also
Sun documentation


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

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
 
GeeCON Prague 2014
 
subject: very urgent...plz help