简体   繁体   English

单击按钮呼叫泽西客户

[英]Calling jersey client by button click

I have a jersey client class which is making a put request to a rest. 我有一个泽西岛客户班,正在提出休息请求。

//JerseyClient // 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. 并且此按钮位于我的jsp文件之一上。 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. 因此,有没有一种方法可以在单击按钮时在jsp文件中调用此请求,并根据其余响应将页面重定向到新的jsp中。

No directly by the jsp. 没有直接由jsp。 If you include this code in the jsp, it will be executed while parsing the jsp page in the server. 如果将此代码包含在jsp中,它将在解析服务器中的jsp页面时执行。

You should include it in a Java component which will be called by a Servlet (or controller, filter, etc...). 您应该将其包含在Java组件中,该组件将由Servlet(或控制器,过滤器等)调用。 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. jsp中的按钮将调用执行该组件的URL,在那里您必须对结果做任何需要的事情。

If you want to execute directly in the jsp while clicking the button, you must make it using javascript in client side. 如果要在单击按钮的同时直接在jsp中执行,则必须使用客户端中的javascript使其执行。

You may call this method using URL reference: 您可以使用URL参考来调用此方法:

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. 然后,从jsp页面中,您可以定义按钮以重定向到该URL以启动上述方法。

You may read more about jersey annotations here . 您可以在此处阅读更多有关球衣注解的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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