简体   繁体   中英

How to get a response to HTTPClient from a Servlet which is called from the HTTPClient?

I am able to successfully post to a servlet using HttpClient. However i need a boolean parameter back to the HttpClient from the servlet and am not sure how to do the same.

Earleir i was using the common-httpclient-3.1.jar.However then i was made aware of the version upgrade of httpclient to 4.0 which assists in the Response handling from HttpResponse. Now I am using httpclient.4.0.3, httpcore-4.1 and httpmime-4.1.1. The code being tried by me is as below for the HttpClient.

HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost("http://localhost:9090/drools-guvnor/KeystoneServlet");
HttpEntity entity = method.getEntity();
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("directory", directory));
nvps.add(new BasicNameValuePair("username", username));
nvps.add(new BasicNameValuePair("password", password));
method.setEntity(new UrlEncodedFormEntity(nvps));
ResponseHandler responseHandler = new BasicResponseHandler();
HttpResponse response = client.execute(method, responseHandler);
System.out.println("responseBody: "+responseHandler.handleResponse(response));

In the KeyStoneServlet i am able to successfully get the parameters directory, username and password. Further using these parameter am doing a further processing and am able to retrieve a boolean parameter which i want back as a response to the HttpClient. However i do not know how can i do the same. The responseBody SOP above comes blank and doesnt show me anything.

Any assistance on the same will be highly appreciated.

There are many ways to return data back from the servlet.. JSON, XML, plain text. All you have to do is:

response.setContentType("text/xml");
PrintWriter pw=response.getWriter();
pw.write(<reponse data string xml>);

Receive the data on the httpclient and parse it. If all you need is the boolean then a single string saying true/false should do.. But what if there is an exception you want to throw from your servlet? In that case an XML is more robust.

If dont want to parse xml or text and handle the response, then try using web services..

EDIT: In response to the comment:

In your servlet, you have access to a HTTPResponse object. Let's use response to represent this object.

response.setContentType("text/plain");
PrintWriter pw=response.getWriter();
pw.write(<true or false based on your processing>);

Now in your client code, I havent worked much with httpclient 4.. so this is with httpclient 3 -

HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost("http://localhost:9090/drools-guvnor/KeystoneServlet");
HttpEntity entity = method.getEntity();
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("directory", directory));
nvps.add(new BasicNameValuePair("username", username));
nvps.add(new BasicNameValuePair("password", password));
method.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse httpResponse = client.execute(method);
int statusCode = httpResponse.getStatusLine().getStatusCode(); 
if (statusCode == HttpStatus.SC_OK) {
    InputStream is = httpResponse.getEntity().getContent();

    //convert your stream into a string and convert it into a boolean
}

Sorry I didnt have httpclient 4 to check against.. But I am sure the above should work..

I wouldn't recommend sending plain text back. Instead work with XML and process it the same way as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM