ANDROID_ Login Server Logic

Source: Internet
Author: User

My personal website: www.shichengwu.com

The client logic for ANDROID_ login refers to the loginactivity get account and Password box characters, pass data to the server through the servlet (Loginservlet), and accept feedback to handle the login logic. The following is a detailed description of Loginservlet implementation methods.

1.LoginServlet

Package Com.amaker.servlet;import Java.io.ioexception;import Java.io.printwriter;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.amaker.dao.userdao;import Com.amaker.dao.impl.userdaoimpl;import com.amaker.entity.user;/* * Realize login function * @author SHICHENGWU * @site www.shichengwu.com */public class Loginservlet extends HttpServlet {public void doget (Httpservletreque St request, httpservletresponse Response) throws Servletexception, IOException {response.setcontenttype ("text/html"); PrintWriter out = Response.getwriter (); Userdao dao = new Userdaoimpl ();//Gets the client request parameter string username = Request.getparameter ("username"); String Password = request.getparameter ("password"); System.out.println ("Get user name" +username); SYSTEM.OUT.PRINTLN ("Get Password" +password); User u = dao.login (username, password); if (u!=null) {//Response client content, Login succeeded Out.print (Build (U));} else{//response to client content, Login failed Out.print ("0");} Out.flush (); OUt.close ();} Private String build (User u) {String usermsg = ""; usermsg+= "id=" +u.getid (); usermsg+= ";"; usermsg+= "Name=" +u.gettruename (); return usermsg;} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request,response);} public void Init () throws Servletexception {}public Loginservlet () {super ();} public void Destroy () {Super.destroy ();}}

2. Database encapsulation entity class User

Package com.amaker.entity;/** * Entity class for encapsulating User table * @author Shichengwu * @site www.shichengwu.com */public class User {//number PR ivate int id;//Account private string username;//password private string password;//user name private string truename;//gender public int Geti D () {return ID;} public void setId (int id) {this.id = ID;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String Gettruename () {return truename;} public void Settruename (String truename) {this.truename = Truename;}}

3. Interface Userdao

Package Com.amaker.dao;import com.amaker.entity.user;/* Uesrdao interface * @author Shichengwu * @site www.shichengwu.com */PUBL  IC interface Userdao {//Login method Public User login (string username,string password);p ublic user Login1 (string username, string password);}

4. Database Interaction Implementation Userdaoimpl

Package Com.amaker.dao.impl;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.ResultSet ; Import Java.sql.sqlexception;import Com.amaker.dao.userdao;import Com.amaker.entity.user;import com.amaker.util.dbutil;/** * User Login DAO Implementation class * @author Shichengwu * @site www.shichengwu.com */public class Userdaoimpl Impleme NTS Userdao {/** * Login with user name and password, login successfully returned user object, Login failed return null */public user login (string username, string password) {//query SQL statement St Ring sql = "Select Id,username,password,true_name from Kl_user" + "where username=?" and password=? ";  /Database Connection Tool class Dbutil Util = new Dbutil ();//Get connection Connection conn = Util.openconnection (); try {//Get pre-defined statement PreparedStatement pstmt = conn.preparestatement (sql);//Set query parameters pstmt.setstring (1, username);p stmt.setstring (2, password);//Execute query ResultSet rs = Pstmt.executequery ();//Determine if the user exists if (Rs.next ()) {//get user information int id = rs.getint (1); String truename = rs.getstring (4);//encapsulates user information for users u = new user (); U.setid (ID); U.setusername (username); U.setpassword ( password); u. Settruename (Truename); SYSTEM.OUT.PRINTLN ("Operation a"); return u;}} catch (SQLException e) {System.out.println ("Action B"); E.printstacktrace ();} finally {System.out.println ("Operation C"); UTIL.CLOSECONN (conn);} return null;}


5 JDBC Configuration
Driver=oracle.jdbc.driver.oracledriverurl=jdbc:oracle:thin:@192.168.1.49:1521:orclusername=xxpassword=xx

At this point, the server-side design, coupled with the previous article design of the client logic, a complete landing instance to build success, as for page layout, draw an account input box, a password input box, plus a button click Login, it is easy, login logic, The access verification of the account and password box has been completed in this article and above.

For more android/ios knowledge, please see my personal website: www.shichengwu,com.


ANDROID_ Login Server Logic

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.