0

I'm setting the http status in doPost() of servlet as

if(success)    
{    
  response.setStatus(HttpServletResponse.SC_OK);    
}else{    
  response.sendError(response.SC_BAD_REQUEST, "Message");    
}    

In client side after calling the server as:

HttpResponse aHttpResponseL = client.execute(aHttpPostL);    
BufferedReader aBufferedReaderL = new BufferedReader(new InputStreamReader(
                aHttpResponseL.getEntity().getContent()));   

But always I'm getting null as value.

Thanks in advance.

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
Vignesh
  • 2,295
  • 7
  • 33
  • 41
  • What is the relevance of ServletFilter in this question? Did you get the response code when you used ServletFilter? if so, how? – Vikdor Aug 27 '12 at 07:07
  • no i saw some post using servlet filter but its too long(i didnt try that also). so only – Vignesh Aug 27 '12 at 07:10
  • Take a look at this [SO] question. [http://stackoverflow.com/questions/1782517/how-can-i-retrieve-the-msg-from-httpservletresponse-senderror – Sal Aug 27 '12 at 13:15

1 Answers1

0

Check the status line:

HttpResponse aHttpResponseL = client.execute(aHttpPostL);  
int code = aHttpResponseL.getStatusLine().getStatusCode();
kothvandir
  • 2,111
  • 2
  • 19
  • 34
  • Thanks Kothvandir, its working but how to get the Status message? – Vignesh Aug 27 '12 at 07:16
  • Did you try the getReasonPhrase? http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/StatusLine.html#getReasonPhrase() – kothvandir Aug 27 '12 at 07:23
  • Are you asking for the correspondence between code and "code description text" like 200 OK, 201 CREATED, etc? The HttpStatus Class stores all the http status codes but I don't think there's a class that provides the String description of this codes. – kothvandir Aug 27 '12 at 07:31
  • Ya thats fine, but for error "response.sendError(response.SC_BAD_REQUEST, "Message");" is it possible to get the message? – Vignesh Aug 27 '12 at 07:41
  • A post about how to obtain the message: stackoverflow.com/a/2925207/1297972 – kothvandir Aug 27 '12 at 08:45
  • Sorry kothvandir, Im new to java. I try with the link which u gave above,In getReasonPhrase still im getting http description message. While im using the below code "BufferedReader aBufferedReaderL = new BufferedReader(new InputStreamReader(aHttpResponseL.getEntity().getContent()));" im getting only message not code – Vignesh Aug 28 '12 at 05:29