0

I am sending my Ajax Request in the following format

xmlhttp.open("POST","http://172.16.xx.xx:8080/ajax/validate",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(send); //where send is a string retrieved from textarea

This is my Servlet code

ObjectInputStream in =new ObjectInputStream(request.getInputStream()); 
String inputxmlstring=(String) in.readObject();

I am getting the following exception

java.io.StreamCorruptedException: invalid stream header: 3C3F786D

What is the problem with the code? Is there anything wrong with my request header content type?

EDIT 1

             BufferedInputStream in =new BufferedInputStream(req.getInputStream());
             byte[] buf=new byte[req.getContentLength()];
             while(in.available()>0)
             {
              in.read(buf);
             }
             String inputxmlstring=new String(buf);
             System.out.println(inputxmlstring);

If I use this code for Servlet I get the following error

14:13:27,828 INFO  [STDOUT] [Fatal Error] :1:1: Content is not allowed in prolog
.
14:13:27,843 INFO  [STDOUT] org.xml.sax.SAXParseException: Content is not allowe
d in prolog.


EDIT 2
I use this code to parse. The String inputxmlstring has been used in Edit1.

DocumentBuilderFactory fty1 = DocumentBuilderFactory.newInstance();
fty1.setNamespaceAware(true);
DocumentBuilder builder1 = fty1.newDocumentBuilder();
ByteArrayInputStream bais1 = new ByteArrayInputStream(inputxmlstring.getBytes());
Document xmldoc1=builder1.parse(bais1);
suraj
  • 1,828
  • 12
  • 36
  • 64
  • yoi might want to read this: http://stackoverflow.com/questions/2939073/java-io-streamcorruptedexception-invalid-stream-header-7371007e –  May 25 '12 at 07:00

1 Answers1

1

You should use the ObjectInputStream only if you know the other end it was written using ObjectOutputStream.

When the client uses ObjectOutputStream, it writes special bytes indicating it is object stream. If these bytes are not present ObjectInputStream will throw StreamCorruptedException.

In your case you should read using request.getInputStream() because the XMLHttpRequest is not sending using ObjectOutputStream.

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • How can you read directly using request.getinpustream(). I thnk you will have to wrap it around a bufferedinputstream or something like that. – Ashwin May 25 '12 at 08:13
  • @suraj Can you post the stacktrace – Ramesh PVK May 25 '12 at 09:14
  • @suraj in EDIT1 i do not see you parsing XML which will throw org.xml.sax.SAXParseException: Content is not allowe d in prolog. Something missing, – Ramesh PVK May 25 '12 at 09:16
  • What response string you are getting? Can you print it? I think it not in XML format. – Ramesh PVK May 25 '12 at 09:29
  • @RameshPVK response string in Servlet or AJAX ? – suraj May 25 '12 at 09:36
  • @RameshPVK In servlet I tried printing the string but it printed only blank spaces – suraj May 25 '12 at 09:37
  • In Servlet? First you have to verify if you are receiving any data from client? and the data has to be XML format to get parsed. As you are saying the data is blank space, that is the whole issue. Check why are you getting blank space? You should not parse when you have blank space. – Ramesh PVK May 25 '12 at 09:50
  • @RameshPVK The data I am sending is in xml format only. When I print data while sending it gets printed in client side(as alert in Javascript) but at the server end it is just printing blankspaces – suraj May 25 '12 at 09:59