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.

1 Answer 1

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);
...
Sign up to request clarification or add additional context in comments.

3 Comments

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...
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. mvnrepository.com/artifact/org.apache.httpcomponents
You can try with JRE API instead of using external libraries. stackoverflow.com/questions/4349854/…

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.