1

servelt code

System.out.println(" ================servlet==================");
InputStream in = request.getInputStream();
int a = in.available();
byte[] b = new byte[a];
in.read(b);
String stringValue = new String(b,"utf-8");
System.out.println("receive data==="+stringValue);
OutputStream dataOut = response.getOutputStream();
String responseData = "<test>test</test>";
System.out.println("response datea==="+responseData);
dataOut.write(responseData.getBytes("utf-8"));
dataOut.flush();
dataOut.close();

client code

System.out.println("================client======================");
java.net.URL url = new java.net.URL("test address");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
String sendData = "<data>send</data>";
System.out.println("send data="+sendData);
OutputStream dataOut = con.getOutputStream();
dataOut.write(sendData.getBytes("utf-8"));
dataOut.flush();
dataOut.close();
InputStream in = con.getInputStream();
int a = in.available();
byte[] b = new byte[a];
in.read(b);
String stringValue = new String(b,"utf-8");
in.close();
System.out.println("receive data="+stringValue);

I get the print results servlet console ================servlet================== receive data=== response datea===test

client console

================client======================
send data=<data>send</data>
receive data=<test>test</test>

My question is that servlet can't receive the data from the client

who can help me?

3
  • Whats the error you are getting? Commented Jun 20, 2013 at 6:24
  • Then what is the reason you posted it here.Dealing with people in right manner matters !!! Commented Jun 20, 2013 at 6:27
  • but i can get the data from request.getParamenter("") Commented Jun 20, 2013 at 6:27

2 Answers 2

4

My question is that servlet can't receive the data from the client

It may not be the only problem, but this code is completely broken:

int a = in.available();
byte[] b = new byte[a];
in.read(b);

You're assuming that all the data is available right at the start. You should instead be reading from the stream until it runs out of data. Given that you want the result as text, I'd wrap the stream in an InputStreamReader and read from there. For example:

BufferdReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
    System.out.println("Servlet read line: " + line);
}

If you actually want to read it as XML, you should be able to pass the InputStream (or Reader) to an XML parser library to create a DOM.

You should be doing the same thing in the client code too, by the way. Basically:

  • Never ignore the return value of InputStream.read
  • Avoid using available(); it's rarely appropriate
  • Use an InputStreamReader to read text from a stream, rather than constructing it yourself from the bytes
  • Use an XML API to read XML rather than handling it as raw text
Sign up to request clarification or add additional context in comments.

2 Comments

If I do this Document document = null; SAXReader reader = new SAXReader (); document = reader.read (new InputStreamReader (conn.getInputStream (), "UTF-8")); I get the exception org.dom4j.DocumentException: Error on line -1 of document : Premature end of file. Nested exception: Premature end of file. thanks
@fenger: That suggests the data isn't actually getting to the servlet then. Why are you using DataOutputStream in the client? It seems pointless to me. I suggest you use Wireshark to diagnose what's happening at a network level.
0

As of now I can see that the value of int b is 0 so it is not reading any data from the input stream.

According to this documentation

available

will always return 0 for InputStream which has been extended byt the ServletInputStream. As told by Jon or Edit:

InputStream is=request.getInputStream();
OutputStream os=response.getOutputStream();
byte[] buf = new byte[1024];
int chunk = is.read(buf);

1 Comment

so how can I get the data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.