Use Java business logic with MySQL for Logon registration (XMPP protocol)

Source: Internet
Author: User
XMPP protocol: communication protocol is an agreed rule, and XMPP is also an interface rule. In short, XMPP is a markup text string. For example, after msgsendhxjavasendcontenthelloworldcontentmsg has learned the XMPP protocol, it implements the CS login registration operation. This login operation is not like the previous one.

XMPP protocol: communication protocol is an agreed rule, and XMPP is also an interface rule. In short, XMPP is a markup text string. For example, after msgsendhxjava/sendcontenthelloworld/content/msg has learned the XMPP protocol, the C/S logon registration operation is implemented. This login operation is not like the previous one.


XMPP protocol:


Communication Protocol is an agreed rule, and XMPP is also an interface rule. In short, XMPP is a text string in the tag format.


Example:

Hxjava Helloworld


After learning the XMPP protocol, the C/S login registration operation is implemented. This login operation does not use some Map containers to store data as before.


The database is used to store data, which not only facilitates data management and maintenance, but also prevents data loss. When a Map container is created for storage, the program closes the data and disappears.


I write a C/S login application to the XMPP protocol:








Because it is just a practice, the code is relatively simple, and the database stores only five pieces of information, user information:

Id, name, password, tellphone, email


Create a table:


I use powerDesigner of version 16.5.



After creating the table, export it in SQL format, and run the SQL language with Navicat for MySQL software. Then an empty table is obtained.




The process of writing a java program is not much said. Wait until the code is sent out and explained again.

First look at the process


Interface and code test:


On the registration page, when I enter the registration information and press the Registration button, connect to the server and send an XMPP protocol string to the server. The server parses the string and then adds it to the database, an EOF exception is the reason why the connection is closed.




After registration, there are two accounts in the Table. Test the logon system:

Enter a correct account password:



Enter the second correct account password:



Enter an incorrect username and password for the third time:



Summary of logon to the registration system:


The test of this project has been completed, and all the basic functions have been implemented, reflecting the agreement and resolution of the XMPP protocol, and implementing the combination of the dao layer and Java business logic, use the data in the database to verify the user, making the program more reliable and more maintainability.



Code:

The core code of the server is the socket connection and service, which must be resolved through the XMPP protocol. Because we only need to test the login function, so I will not create too many classes to encapsulate them, the code added for authentication login and registration is encapsulated in the method, and a client Thread class SocketThread is customized. The run method uses an endless loop to execute the login or registration method:


Run Method


public void run() {try{String Msg = null;while(true){Msg = ois.readUTF();String type = this.getType(Msg, "type");if("createUser".equals(type)){this.CreatUser(Msg);}else if("loginUser".equals(type)){this.LoginUser(Msg);}Msg = null;}}catch(Exception e){e.printStackTrace();}}


Method for parsing XMPP:

Public String getType (String str, String type) {// process the String System. out. println ("The getType method has been entered, and the obtained Msg is:" + str); int beginIndex = str. indexOf ("<" + type + ">"); int endIndex = str. indexOf ("
 "); String value = str. substring (beginIndex + 2 + type. length (), endIndex); System. out. println ("the obtained value is:" + value); return value ;}

How to register a new user:

Public void CreatUser (String Msg) {System. out. println ("the server has entered the CreatUser method... "); String name = this. getType (Msg, "name"); String password = this. getType (Msg, "psw"); String tellphone = this. getType (Msg, "tel"); String email = this. getType (Msg, "email"); System. out. println (name + "" + password + "" + tellphone + "" + email); UserDatapojo udp = new UserDatapojo (2, name, password, tellphone, email ); try {new UserDatadaoimpl (). insert (udp); System. out. println ("the data has been stored in the database... "); oos. writeUTF ("User Registration successful... "); oos. flush ();} catch (Exception e) {e. printStackTrace ();}}

Logon method:

Public void LoginUser (String Msg) throws Exception {String name = this. getType (Msg, "name"); String password = this. getType (Msg, "psw"); boolean s = new UserDatadaoimpl (). query (name, password); if (s) {System. out. println ("Logon successful... "); oos. writeUTF ("Logon successful... "); oos. flush (); return;} System. out. println ("Logon failed... "); oos. writeUTF ("Logon failed... "); oos. flush ();}

Client registration method:

Public void actionreceivmed (ActionEvent e) {if ("register". equals (e. getActionCommand () {String type ="
 
  
CreateUser
 "; String name ="
 
  
"+ NameField. getText () +"
 "; String psw ="
 
  
"+ PswField. getText () +"
 "; String tel ="
 
  
"+ TelField. getText () +"
 "; String email ="
 
  
"+ MailField. getText () +"
 "; String CreatUserMsg ="
 
  
"+ Type + name + psw + tel + email +"
 "; System. out. println (CreatUserMsg); Client socket = Client. getInstance (); socket. getConnection (); System. out. println ("the client is connected to the server... "); try {socket. getOos (). writeUTF (CreatUserMsg); socket. getOos (). flush (); System. out. println ("XMPP protocol sent to the server... "); System. out. println (socket. getOis (). readUTF (); socket. closeConnection ();} catch (Exception e1) {e1.printStackTrace ();} System. out. println ("the registration function is executed .. ");}}

Client logon method:

If ("Log On". equals (e. getActionCommand () {String type ="
 
  
LoginUser
 "; String name ="
 
  
"+ UserField. getText () +"
 "; String psw ="
 
  
"+ PswField. getText () +"
 "; String CreatUserMsg ="
 
  
"+ Type + name + psw +"
 "; System. out. println (CreatUserMsg); Client socket = Client. getInstance (); socket. getConnection (); System. out. println ("the client is connected to the server... "); try {socket. getOos (). writeUTF (CreatUserMsg); socket. getOos (). flush (); System. out. println ("XMPP protocol sent to the server... "); System. out. println (socket. getOis (). readUTF (); socket. closeConnection ();} catch (Exception e1) {e1.printStackTrace ();} System. out. println ("the login function is executed .. ");}

For more information about the XMPP protocol, see:

XMPP ppt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.