Call REST GET Service from JSP

2019-04-09 21:28发布

问题:

I have a JSP that dynamically sets the page header of my application.

However, I want to be able to call the REST Service that gets user details based on the system user. I already have the system user value but need to call the backend service to get the details from the database. This is already implemented but I don't know how to setup the JSP to do this.

I do not want to use javascript as this is being used for the extjs side of things.

回答1:

In order to call REST from JSP, you could utilize Apache HTTPClient. Once you have that you could walk through the samples as well as the HTTPClient Tutorial. HTTPClient supports all REST API Call including GET/POST and others.

Check also this following HTTPClient template to see how HTTPClient can be used with REST. You need to call a similar code from your JSP.

In particular to REST GET Service, you want to look the following block from the template in the above link


final HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000);
HttpGet httpget = new HttpGet(SERVER_URL + url);
HttpResponse response = httpClient.execute(httpget);


标签: java jsp rest get