当前位置:网站首页>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 !
边栏推荐
- LeetCode 1566. Repeat the pattern with length m at least k times
- Jedis
- [unsolved]7-14 calculation diagram
- ~Introduction to form 80
- LeetCode 1637. The widest vertical area between two points without any point
- 100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
- JS encapsulates the method of array inversion -- Feng Hao's blog
- Spark independent cluster dynamic online and offline worker node
- LeetCode 1584. Minimum cost of connecting all points
- Gridhome, a static site generator that novices must know
猜你喜欢
图像处理一百题(1-10)
谢邀,人在工区,刚交代码,在下字节跳动实习生
MP4格式详解
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
Sublime text code formatting operation
~86m rabbit practice
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
FLV格式详解
Chapter 2 shell operation of hfds
LeetCode 1641. Count the number of Lexicographic vowel strings
随机推荐
Restful style interface design
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
Shell_ 06_ Judgment and circulation
第一章 MapReduce概述
Solve the single thread scheduling problem of intel12 generation core CPU (II)
图像处理一百题(11-20)
7-7 ring the stupid bell
Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
Market trend report, technological innovation and market forecast of desktop electric tools in China
Basic principles of video compression coding and audio compression coding
Chapter III principles of MapReduce framework
7-12 inventory code base
string. How to choose h and string and CString
LeetCode 1640. Can I connect to form an array
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
Spark independent cluster dynamic online and offline worker node
Codeforces Round #771 (Div. 2)
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
~73 other text styles
Chapter 5 detailed explanation of consumer groups