All article source code has been uploaded to Github as a whole package, for the stars!


directory

Fdog series (1) : If you want to write a chat software, start with the QQ registration page.

Fdog series ii: How to do after the HTML write registration page, using Java to write background response.

Source code appears in the article: leave an email address in the comments section.

Creation is not easy, you see the officer point a concern, point a praise!


1. Introduction

The previous two articles respectively using HTML and wrote a front page using Java back-end response code written, has realized the front-end and back-end data interaction, this article will introduce how to write the data into the database, and implement to send that message authentication code and how to deploy to the server, need to use MYSQL, tencent SMS service API, cloud servers, Tomcat.

All article source code has been uploaded to Github as a whole package, for the stars!


2. Use Tencent Cloud SMS interface to send SMS messages

I find a lot of SMS interface service, or need to one-time buy thousands of, or that requires companies to buy, around a few laps, and finally found free text messages on the tencent cloud here, new users can receive free article 100, and later will send each month, and there’s no need to enterprise users can use, found a link to you, Pick it up yourself. Tencent cloud SMS free trial

I’ll assume you’ve picked up the message, go down, click the console, and search for the message to go to the page below.Click domestic SMS, signature management, create signature, choose signature for self-use, signature type for website, and then will let you upload website record background screenshots, application instructions to write learning to use SMS services.

It takes at least three months for the server to be logged

Create a body template. {1} in the template is the verification code we want to send. The application will be successful in about 30 minutes.Click on the list of apps. If there is no default app, you can create one yourselfClick the application, and there will be a SDKAppID and App Key. These two codes will be used as important certificates for SMS sending and should be kept properly.

Click on theAccess managementTo obtain the key pair

Once you’ve done all the above, click on the SMS documentation. There are download files for the SDK, including Maven and GitHub. Bloggers use the first one, but it took some effort to use it for the first time. Tencentcloud-sdk-java-3.1.217.jar can be used directly if you don’t mind

Then look at the implementation of the specific code

String secretId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String secretKey= "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// ID of the SMS application
String appid = "xxxxxxxxx";
// SMS signature content
String sign = "The Dog's Little station.";
// ID of the SMS template
String templateID = "xxxxxx";
+86 is the country code, 182******** is the mobile phone number, the maximum number of mobile phone number is 200
String[] phoneNumbers = { "+ 86"+phone};
// Template parameters: If there are no template parameters, this parameter is null
String[] templateParams = {String.valueOf(randnum)};{1} randnum is a random five-digit number generated by me
try {
// Necessary steps: instantiate an authentication object. Enter the secretId and secretKey of the Tencent cloud account
Credential cred = new Credential(secretId , secretKey);
ClientProfile clientProfile = new ClientProfile();
// THE SDK uses TC3-hMAC-SHA256 for signature by default. Do not change this field unless necessary
clientProfile.setSignMethod("HmacSHA256");
// The second argument to the client object that instantiates the SMS is the location information, which can be filled directly with the string ap-guangzhou or referenced with a preset constant
SmsClient client = new SmsClient(cred, "", clientProfile);
// Instantiate a request object, depending on the interface called and the actual situation, you can further set the request parameters you can directly query the SDK source code to determine which interface properties can be set
SendSmsRequest req = new SendSmsRequest();
// SMS application ID: indicates the actual SDKAppID generated after the application is added to [SMS Console], for example, 1400006666
req.setSmsSdkAppid(appid);
// SMS signature content: Use UTF-8 encoding and enter the approved signature. You can log in to [SMS console] to view the signature information
req.setSign(sign);
// SMS template ID: Enter the ID of the approved TEMPLATE. You can log in to the SMS Console to view the TEMPLATE ID
req.setTemplateID(templateID);
// Deliver the mobile phone number using E. 164 standard and +[Country or region code][Mobile phone number], for example, +8613711112222
req.setPhoneNumberSet(phoneNumbers);				
req.setTemplateParamSet(templateParams);
// Initiate the request by calling the SendSms method from the client object. Note that the request method name corresponds to the request object. The returned RES is an instance of the SendSmsResponse class that corresponds to the request object
SendSmsResponse res = client.SendSms(req);
// Outputs a string in JSON format
System.out.println(SendSmsResponse.toJsonString(res));
// You can retrieve a single value. You can view the definition of the return field through the interface document on the official website or jump to the definition of the Response object
System.out.println(res.getRequestId());
Copy the code

SMS effect:! [insert picture description here] (img – blog. Csdnimg. Cn / 20210413201… =300x) after registering successfully, let’s see how to write data to the database.


3. Java connects to the database

Please download the JDBC I prepared for you first.

This article may help you if there are some errors in the connection process. Java connection to MySQL and the problems that occur

// Load the driver
try {
	Class.forName("com.mysql.jdbc.Driver");
	System.out.print("Driver loaded successfully,");
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
127.0.0.1 can be either changed to your server's IP address or written to the local IP address, since the server will eventually be run on. 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("Database connection successful"); }}catch (SQLException e) {
		System.out.println("Database link failed:"+e.getMessage());
	}

// Use the data we learned from the front end and 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 randomly generates an 8-digit 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 succeeded");
			
	// Invoke the registration success page
	}catch(SQLException e){
		System.out.println("Write failed");
	}	
Copy the code

4. Deploy to the server (if you have one)

Eclipse click File, Export Export, choose WAR File Export, remember to check the red box below! [insert picture description here] (img – blog. Csdnimg. Cn / 20210413205… =300x)

Use the file upload tool to upload the packaged WAR file to the Tomcat Webapps folder on the server.

If you don’t have an upload tool, click Download

If Tomcat is not configured, click Tomcat Configuration Tutorial

Run the./shutdown.sh command to stop the Tomcat service, and then run the./start.sh command to start the Tomcat service. Wait a few seconds, and you can access the Tomcat service.

Click to visit this case: Fdog

Say it again at the end of the article! Want to source can leave a mailbox in the comments area, with a small thumbs-up!

The next content will use Qt to write similar to QQ client login interface, the first picture, including a lot of small skills, pay attention to me, do not get lost oh!

See you next time!