当前位置:网站首页>Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
2022-07-06 16:52:00 【Flower dog fdog】
List of articles
Catalog
Fdog series ( One ): reasoning , It's better to write a chat software , Let's start with imitation QQ Let's start the registration page .
Fdog series ( Two ):html What to do after writing the registration page , use java Write backstage response .
The way to get the source code in this article : Leave your email address in the comments section .
It's not easy to create , Please pay attention , A great bai !
1. Preface
The first two articles use respectively html Write the front page and use java Write back-end response code , The data interaction between the front end and the back end is realized , Today's article will show you how to write data to a database , And realize the sending of SMS verification code and how to deploy it to the server , What needs to be used is MYSQL, Tencent SMS service API, Cloud server ,Tomcat.
The source code of all articles has been packaged and uploaded to github, Seeking stars !
2. Use Tencent cloud SMS interface to send SMS
I found a lot of people who provide SMS interface services , Or you need to buy thousands at a time , Or you need companies to buy , Go around for several turns , Finally, I found a free SMS here in Tencent cloud , New users can get it for free 100 strip , And every month after that , And it doesn't need enterprise users to use , The link has been found for you , Get it yourself . Tencent cloud SMS free trial
I'll take it as if you've got the text message , Keep going down , Click on the console , And then search for text messages , Go to the following page .
Click domestic SMS , Signature management , Create signature , Choose signature for your own use , The signature type is website , Then it will let you upload the background screenshot of the website , Just write the application instructions and learn to use SMS service .
It takes at least three months for the server to record
Create a body template , In the template {1} It's the verification code we're going to send , almost 30 The application will be successful in 15 minutes .
Click on the application list , If there is no default app , You can create your own
Click application , There is one SDKAppID and App Key, These two codes will be used as important credentials for SMS sending , Keep it safe .
Click on Access management , Get the key pair
After all the above work is done , Click on SMS documents , It's about SDK Download the document , Include Maven and GitHub Two kinds of , Bloggers use the first , It took a lot of effort to use it for the first time ,Maven It's just the integration of packages , Then I'll download it to you directly , If it's too much trouble, you can use the package I downloaded tencentcloud-sdk-java-3.1.217.jar
Then look at the implementation of the specific code
String secretId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String secretKey= "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// SMS app ID
String appid = "xxxxxxxxx";
// SMS signature content
String sign = " Flower dog's station ";
// SMS template ID
String templateID = "xxxxxx";
//+86 For the country code ,182******** For cell phone number , Don't exceed at most 200 Mobile phone number
String[] phoneNumbers = {
"+86"+phone};
// Template parameter : If there is no template parameter , Then set to empty
String[] templateParams = {
String.valueOf(randnum)};// In the corresponding template {1} randnum Random five digits generated for me
try {
// Necessary steps : Instantiate an authentication object , The key pair of Tencent cloud account is required to enter the parameter secretId and secretKey
Credential cred = new Credential(secretId , secretKey);
ClientProfile clientProfile = new ClientProfile();
//SDK The default with TC3-HMAC-SHA256 To sign Do not modify this field unless it is necessary
clientProfile.setSignMethod("HmacSHA256");
// Instantiation SMS Of client object The second parameter is geographic information , You can fill in the string directly ap-guangzhou, Or reference a preset constant
SmsClient client = new SmsClient(cred, "", clientProfile);
// Instantiate a request object , According to the calling interface and the actual situation , You can further set the request parameters You can directly inquire SDK The source code determines which properties of the interface can be set
SendSmsRequest req = new SendSmsRequest();
// SMS app ID: stay [ SMS console ] Add the actual generated after the application SDKAppID, for example 1400006666
req.setSmsSdkAppid(appid);
// SMS signature content : Use UTF-8 code , The approved signature must be filled in , You can log in [ SMS console ] Check signature information
req.setSign(sign);
// SMS template ID: The approved template must be filled in ID, You can log in [ SMS console ] View templates ID
req.setTemplateID(templateID);
// Send out your mobile number , use e.164 standard ,+[ Country code ][ cell-phone number ] for example +8613711112222
req.setPhoneNumberSet(phoneNumbers);
req.setTemplateParamSet(templateParams);
// adopt client Object call SendSms Method to initiate a request . Note that the request method name corresponds to the request object Back to res It's a SendSmsResponse Class , Corresponding to the request object
SendSmsResponse res = client.SendSms(req);
// Output JSON Format string package
System.out.println(SendSmsResponse.toJsonString(res));
// You can take out a single value , You can use the official website interface document or jump to response Object to view the definition of the return field
System.out.println(res.getRequestId());
SMS effect :
After successful registration , Let's see how to write data to the database .
3. java Connect to database
Please download what I have prepared for you first JDBC.
If there are some errors in the connection , This article may help you .Java Connect MySQL And the problems
// Load driver
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.print(" Driver loaded successfully ,");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Connect to database 127.0.0.1 It can be changed to your server ip You can also write local ip, Because it's going to run on the server in the end ,fdogsql Is the name of the database
String str = "jdbc:mysql://127.0.0.1:3306/fdogsql?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
Connection conn=null;
try {
conn=DriverManager.getConnection(str,"root","xxxxxx");
if (!conn.isClosed()) {
System.out.println(" Successfully connected to database ");
}
} catch (SQLException e) {
System.out.println(" Failed to link database : "+e.getMessage());
}
// Using the data we learned from the front end in the last article , Then write it to the database
String username = request.getParameter("username");
String password = request.getParameter("password");
String phone = request.getParameter("phone");
String account = String.valueOf(num.ReturnRandNumer_2());//ReturnRandNumer_2 The function randomly generates a 8 Number of digits as account number
try{
String sql="insert into user(account,phone,name,password) values("+"'"+account+"',"+"'"+phone+"',"+"'"+username+"',"+"'"+password+"'"+")";
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println(" Write successfully ");
// Call registration success page
}catch(SQLException e){
System.out.println(" Write failure ");
}
4. Deploy to server ( If you have )
Eclipse Click on File,Export export , choice WAR File export , Remember to check the red box below
Use the file upload tool , Will be packed WAR file , Upload to server Tomcat Of webapps Under the folder .
If there is no upload tool , Click on download
If not already configured Tomcat, Click on Tomcat Configuration tutorial
After uploading the file ,Tomcat Will automatically WAR File decompression , Use the command to enter bin Catalog , Use command ./shutdown.sh close tomcat service , And then use ./start.sh Turn on tomcat service , Just a few seconds , Can access .
Click to visit this case :Fdog
At the end of the article ! If you want the source code, please leave your email in the comments area , Give me a little praise !
The next article will use Qt Writing is similar to QQ The client login interface of , Previous picture , It includes a lot of small skills , Pay attention to me , Don't get lost !
See you next time !
边栏推荐
- 【锟斤拷】的故事:谈谈汉字编码和常用字符集
- Cmake Express
- Gridhome, a static site generator that novices must know
- Educational Codeforces Round 122 (Rated for Div. 2)
- (multiple methods, need to continue to see) 7-11 go deep into the tiger's Den
- 7-10 punch in strategy
- ~70 row high
- Monomer application concept
- 谢邀,人在工区,刚交代码,在下字节跳动实习生
- Shell_ 04_ Shell script
猜你喜欢
Chapter 1 overview of MapReduce
ByteDance new programmer's growth secret: those glittering treasures mentors
字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题
Shell_ 06_ Judgment and circulation
Data config problem: the reference to entity 'useunicode' must end with ';' delimiter.
Solve the problem that intel12 generation core CPU single thread only runs on small cores
第5章 NameNode和SecondaryNameNode
The concept of spark independent cluster worker and executor
Hbuilder x format shortcut key settings
LeetCode 1020. Number of enclaves
随机推荐
Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
One hundred questions of image processing (11-20)
7-8 likes (need to continue to improve)
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
Spark independent cluster dynamic online and offline worker node
Chapter 6 datanode
~71 abbreviation attribute of font
~81 long table
~78 radial gradient
Chapter 2 shell operation of hfds
ByteDance new programmer's growth secret: those glittering treasures mentors
7-7 ring the stupid bell
SQL quick start
The 116 students spent three days reproducing the ByteDance internal real technology project
Chapter 7__ consumer_ offsets topic
力扣leetcode第 280 场周赛
~79 Movie card exercise
One hundred questions of image processing (1-10)
Ffmpeg command line use