0

hello my controller is

@Controller
public class HomeController{ 
@RequestMapping(value = "/inString={name}", method = RequestMethod.GET)

      public @ResponseBody String getGreeting(@PathVariable String name, String result) {

      //below is the url has given.I want to pass the name string to the below url for the input_text parameter
      //http://ipaddress/input?input_text=tell me something&target_val=hi;

       result=the_result_provided_from_the_above_URL ; 
        return result;
}
}

I am able to get the pathVariable name.Now I want to pass the name to another URL which returns a string and that string will be returned from the controller.I tried but have not been able to call an URL from the controller.

Mithun Debnath
  • 588
  • 1
  • 8
  • 23

1 Answers1

1

You can call a servlet using Apache's HttpClient. This is an example:

...
private static final String SERVLETURL = "http://ipaddress/input?input_text=tell";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(SERVLETURL);
CloseableHttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
String responseBody = getStringFromInputStream(response.getEntity().getContent());
//Processing here
EntityUtils.consume(entity);
...
Fran Montero
  • 1,679
  • 12
  • 24
  • sir what package do I need to import as It is giving me error and asking me to create class for CloseableHttpClient and HttpGet and for others... – Mithun Debnath Jul 17 '15 at 10:12
  • You need apache httpclient and httpcore for covering my full example. This is the maven repository where you cand find pom reference and download the jars manually. http://mvnrepository.com/artifact/org.apache.httpcomponents – Fran Montero Jul 17 '15 at 10:16
  • You can try with JRE API instead of using external libraries. http://stackoverflow.com/questions/4349854/calling-a-servlet-from-a-java-application – Fran Montero Jul 17 '15 at 10:45