Java business logic with MySQL for login registration (Application of XMPP protocol)

Source: Internet
Author: User
Tags gettext thread class


XMPP Protocol:


Communication protocol is an agreed rule, and XMPP is not an interface rule. Simply put, the XMPP protocol is a text string in a tagged format.


Example:

<msg><send>hxjava</send><content>helloworld</content></msg>


After learning the XMPP protocol, the login registration operation of C/S is implemented, and this time the login operation does not store the data in some map containers as before.


Storage data to use the database, so that not only easy to maintain data management, and data can not be easily lost, the establishment of a map container storage, the program shutdown data disappeared.


I write my own c/s login program to the XMPP protocol that is applied:

<msg>
<name></name>
<psw></psw>
<tel></tel>
<email></email>
</msg>


Because just do the exercise, so the code is relatively humble, database storage information only 5, user information:

Id,name,password,tellphone,email


Create a table:


I'm using the 16.5 version of PowerDesigner.



After creating the table, export it in SQL language format and then run the SQL language with the navicat for MySQL software and then get an empty table.




The process of writing Java does not say much, wait for the code to be sent directly to explain.

First look at the process


interface and code testing:


Registration interface, when I fill out the registration information by pressing the Register button, connect the server, then send a string of XMPP protocol to the server, the server resolves, and then add to the database, the EOF exception is the reason for shutting down the connection.




After registering the table has two accounts, test the login system:

Enter a correct password for your account:



Enter the second correct account password:



Enter an incorrect user name and password for the third time:



Summary of Login registration system:


The test of this project has been completed, the basic functions have been fully realized, embodies the protocol and resolution of the XMPP Agreement, the implementation of the DAO layer and Java business logic, with the data in the database to verify the user, so that the program more reliable, more maintainable.



Code:

The Server Core code is the socket connection and the service, there is the XMPP protocol to resolve, because just to test registration in the login function, so I do not create too many classes to encapsulate, verify the login and registration of the added code is just encapsulated in the method, Customize a client thread class Socketthread, where the Run method uses a dead loop to perform a 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 ();}}


methods for resolving XMPP:

public string GetType (string str,string type) {//Handles string System.out.println ("has entered the GetType method, gets the msg:   " +str); int Beginindex = Str.indexof ("<" +type+ ">"), int endIndex = Str.indexof ("</" +type+ ">"); String value = str.substring (Beginindex+2+type.length (), endIndex); System.out.println ("The value obtained 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 ("Data already stored in database ..."); Oos.writeutf ("User registration succeeded ..."); Oos.flush ();} catch (Exception e) {e.printstacktrace ();}}

how to log in:

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 ("Login successful ..."); Oos.writeutf ("Login successful ..."); Oos.flush (); return;} SYSTEM.OUT.PRINTLN ("Login failed ..."); Oos.writeutf ("Login failed ..."); Oos.flush ();}

How to register the client:

public void actionperformed (ActionEvent e) {if ("registered". Equals (E.getactioncommand ())) {String type = "<type> Createuser</type> "; String name = "<name>" +namefield.gettext () + "</name>"; String PSW = "<psw>" +pswfield.gettext () + "</psw>"; String Tel = "<tel>" +telfield.gettext () + "</tel>"; String email = "<email>" +mailfield.gettext () + "</email>"; String creatusermsg = "<msg>" +type+name+psw+tel+email+ "</msg>"; System.out.println (CREATUSERMSG); Client socket = client.getinstance (); Socket.getconnection (); SYSTEM.OUT.PRINTLN ("Client is connected to server ..."), try {socket.getoos (). writeUTF (creatusermsg); Socket.getoos (). Flush (); System.out.println ("The XMPP protocol has been sent to the server ..."); System.out.println (Socket.getois (). readUTF ()); Socket.closeconnection ();} catch (Exception E1) {e1.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Registered function performed:");}

Login method for client:

if ("Login". Equals (E.getactioncommand ())) {String type = "<type>loginUser</type>"; String name = "<name>" +userfield.gettext () + "</name>"; String PSW = "<psw>" +pswfield.gettext () + "</psw>"; String creatusermsg = "<msg>" +type+name+psw+ "</msg>"; System.out.println (CREATUSERMSG); Client socket = client.getinstance (); Socket.getconnection (); SYSTEM.OUT.PRINTLN ("Client is connected to server ..."), try {socket.getoos (). writeUTF (creatusermsg); Socket.getoos (). Flush (); System.out.println ("The XMPP protocol has been sent to the server ..."); System.out.println (Socket.getois (). readUTF ()); Socket.closeconnection ();} catch (Exception E1) {e1.printstacktrace ();} System.out.println ("Performed logon function:");

to learn more about the XMPP protocol, see:

The PPT of XMPP


Java business logic to implement login registration with MySQL (Application of XMPP protocol)

Related Article

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.