请问如何在jsp中调用JAVA实现短信发送功能?

yzp625 2025-05-17 20:42:13

我在阿里云里申请了短信发送的服务,对方只提供了java代码,我根据我的实际情况,改了一下,Sample.java代码如下:
package com.aliyun;

import com.aliyun.tea.*;


public class Sample { 

       public static String dxtel;
       public static String dxname;
       public static String dxkalei;
       public static String dxkahao;
       public static String dxxfdate;
       public static String dxyecs;

       public String getDxtel() {
          return dxtel;
        }
 
       public void setDxtel(String dxtel) {
          this.dxtel = dxtel;
        }     

        public String getDxname() {
          return dxname;
        }
 
       public void setDxname(String dxname) {
          this.dxname = dxname;
        }

        public String getDxkalei() {
          return dxkalei;
        }
 
       public void setDxkalei(String dxkalei) {
          this.dxkalei = dxkalei;
        }
      
        public String getDxkahao() {
          return dxkahao;
        }

       public void setDxkahao(String dxkahao) {
          this.dxkahao = dxkahao;
        }
 
        public String getDxxfdate() {
          return dxxfdate;
        }

       public void setDxxfdate(String dxxfdate) {
          this.dxxfdate = dxxfdate;
        }

        public String getDxyecs() {
          return dxyecs;
        }

       public void setDxyecs(String dxyecs) {
          this.dxyecs = dxyecs;
        }

       

    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
       
        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setCredential(credential);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static void main(String[] args) throws Exception {
       
       String  jsonString1= String.format("{\"name\": \"%s\", \"kalei\": \"%s\", \"kahao\": \"%s\", \"xfdate\": \"%s\", \"yecs\": \"%s\"}", dxname, dxkalei,dxkahao,dxxfdate,dxyecs);
        
        com.aliyun.dysmsapi20170525.Client client = Sample.createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()


                    
                .setPhoneNumbers(dxtel)
                .setSignName("高创")
                .setTemplateCode("SMS_48462****")
                .setTemplateParam(jsonString1);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            com.aliyun.dysmsapi20170525.models.SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
        } catch (TeaException error) {
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }        
    }
}

相关的jar包文件也引入了,Sample.class文件也编译成功了的,我在jsp网页给赋短信参数,index.jsp代码如下:
<%@ page import="com.aliyun.*" %>
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head>
    <title>短信发送</title>
</head>
<body>

<%
    Sample Sample2 = new Sample();
    Sample2.setDxtel("1912247*****");
    Sample2.setDxname("张三");
    Sample2.setDxkalei("次数卡");
    Sample2.setDxkahao("20003");
    Sample2.setDxxfdate("2025-5-10");
    Sample2.setDxyecs("14");
    String result = Sample2.getDxtel();
    out.println("Result: " + result);
    
%>

</body>
</html>

可以实现给Sample.java的参数赋值,也能读取所赋的参数值,请教一下,我的JSP中,需要如何改才可以调用main,实现短信发送功能?谢谢!

...全文
133 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
比特灵 01-08 11:52
  • 打赏
  • 举报
回复

无非就是接口,调用对方提供的接口发数据过去。

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧