This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

very urgent...plz help

 
rajarshi dutta
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rajarshi dutta
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this thread on same issue...

Link

Also
Sun documentation
 
rajarshi dutta
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic