JSP发送短信信息,调用Java类方法

谢了这样一个类方法 ,我想要在JSP调用,并且执行这个类方法

package javaapplication;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;

import org.json.JSONException;
import org.json.JSONObject;
/**
*

  • @author Administrator
    */
    public class Api {
    public static void main(String[] args) {
    Api api = new Api();
    String httpResponse = api.testSend();
    try {
    JSONObject jsonObj = new JSONObject( httpResponse );
    int error_code = jsonObj.getInt("error");
    String error_msg = jsonObj.getString("msg");
    if(error_code==0){
    System.out.println("Send message success.");
    }else{
    System.out.println("Send message failed,code is "+error_code+",msg is "+error_msg);
    }
    } catch (JSONException ex) {
    Logger.getLogger(Api.class.getName()).log(Level.SEVERE, null, ex);
    }

    httpResponse =  api.testStatus();
    try {
        JSONObject jsonObj = new JSONObject( httpResponse );
        int error_code = jsonObj.getInt("error");
        if( error_code == 0 ){
            int deposit = jsonObj.getInt("deposit");
            System.out.println("Fetch deposit success :"+deposit);
        }else{
            String error_msg = jsonObj.getString("msg");
            System.out.println("Fetch deposit failed,code is "+error_code+",msg is "+error_msg);
        }
    } catch (JSONException ex) {
        Logger.getLogger(Api.class.getName()).log(Level.SEVERE, null, ex);
    }
    

    }

    private String testSend(){
    // just replace key here
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter(
    "api","key-9e434e0d08c4154762378561143a1f5c"));
    WebResource webResource = client.resource(
    "http://sms-api.luosimao.com/v1/send.json");
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    formData.add("mobile", "sMSPhone");
    formData.add("message", "验证码:逗比【铁壳测试】");
    ClientResponse response = webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).
    post(ClientResponse.class, formData);
    String textEntity = response.getEntity(String.class);
    int status = response.getStatus();
    //System.out.print(textEntity);
    //System.out.print(status);
    return textEntity;
    }

    private String testStatus(){
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter(
    "api","key-9e434e0d08c4154762378561143a1f5c"));
    WebResource webResource = client.resource( "http://sms-api.luosimao.com/v1/status.json" );
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    ClientResponse response = webResource.get( ClientResponse.class );
    String textEntity = response.getEntity(String.class);
    int status = response.getStatus();
    //System.out.print(status);
    //System.out.print(textEntity);
    return textEntity;
    }
    }

1个回答

我目前的理解,后台一个方法,前端jsp页面出发之后调用,那就是前后端通信。ajax异步,或者form表单提交,反正使用http请求到后台出发方法就好了。前端一般就是填入条件。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问