简体   繁体   中英

Calling jersey client by button click

I have a jersey client class which is making a put request to a rest.

//JerseyClient

public void putRequest() throws Exception{
        reloadUri();
        Response response = target.request(MediaType.APPLICATION_JSON)
                .accept("application/json;charset=UTF-8")
                .header("Content-Type", "application/json")
                .header("Authorization", "Basic OTA1MzAwNjY3MDg2OjZ4dDg5dk50VXdCbg==")
                .put(Entity.entity(sub, MediaType.APPLICATION_JSON),Response.class);


        System.out.println(response);
        if(response.getStatus() == 200) {
            System.out.println("put request using Json is Success");
        }
    }

It's working fine, but I wanted to call this function with a button click. And this button is placed on one of my jsp file. So, is there a way to call this request inside that jsp file when button clicked.And redirect the page into new jsp according to response of rest.

No directly by the jsp. If you include this code in the jsp, it will be executed while parsing the jsp page in the server.

You should include it in a Java component which will be called by a Servlet (or controller, filter, etc...). The button in your jsp will make a call to the URL which executes that component, and is there where you must do whatever you need with the result.

If you want to execute directly in the jsp while clicking the button, you must make it using javascript in client side.

You may call this method using URL reference:

define a path annotation for method:

@Path("/PATH")
    @GET// or @POST
    public void putRequest() throws Exception{
 ...
}

And then, from you jsp page you can define the button to redirect to this URL to initiate the above method.

You may read more about jersey annotations here .

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