Android Tomcat app Server

Source: Internet
Author: User

Then yesterday's http://www.bkjia.com/kf/201203/124792.html write, to achieve the login of the server part. First, you have to get a database and create a table to store the usernames and passwords of all users. When the client sends a query request, the user name and password entered by the user will be uploaded to the server, then we can query in the database. Here, our table has three fields, one ID, one username and one password.

Then the encoding is implemented. First, write a class to encapsulate the user information in the database, as shown below:

[Java] public class User {
Private int id;
 
Private String username;

Private String password;
 
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return username;
}
Public void setName (String name ){
This. name = name;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}
 
}
Public class User {
Private int id;

Private String username;
 
Private String password;

Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return username;
}
Public void setName (String name ){
This. name = name;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}

} Then define the query interface:

[Java] public interface UserDao {
// Logon Method
Public User login (String username, String password );
}
Public interface UserDao {
// Logon Method
Public User login (String username, String password );
} Implement this interface:

[Java] public class UserDaoImpl implements UserDao {
Public User login (String account, String password ){
// Query SQL statements
String querySql = "select id, username, password" +
"From userTable" +
"Where username =? And password =? ";
DBUtil util = new DBUtil ();
Connection conn = util. openConnection ();
Try {
PreparedStatement state = conn. prepareStatement (querySql );
State. setString (1, username );
State. setString (2, password );
 
ResultSet result = state.exe cuteQuery ();
If (result. next ()){
 
Int id = result. getInt (1 );
String name = result. getString (4 );

User user = new User ();

User. setId (id );
User. setName (username );
User. setPassword (password );
 
Return user;
}
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Util. closeConn (conn );
}
Return null;
}
 
}
Public class UserDaoImpl implements UserDao {
Public User login (String account, String password ){
// Query the SQL statement www.2cto.com
String querySql = "select id, username, password" +
"From userTable" +
"Where username =? And password =? ";
DBUtil util = new DBUtil ();
Connection conn = util. openConnection ();
Try {
PreparedStatement state = conn. prepareStatement (querySql );
State. setString (1, username );
State. setString (2, password );

ResultSet result = state.exe cuteQuery ();
If (result. next ()){

Int id = result. getInt (1 );
String name = result. getString (4 );

User user = new User ();

User. setId (id );
User. setName (username );
User. setPassword (password );

Return user;
}
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Util. closeConn (conn );
}
Return null;
}

} Finally, servlet is implemented, mainly to process the query requests from the client and return the query results:

[Java] public class Login extends HttpServlet {
 
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
UserDao dao = new UserDaoImpl ();
// Obtain the client request parameters
String username = request. getParameter ("username ");
String password = request. getParameter ("password ");

User u = dao. login (username, password );
If (u! = Null ){
Out. print ("Logon successful ");
} Else {
Out. print ("null ");
}
Out. flush ();
Out. close ();
}

 
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 ();
}
 
}
Public class Login extends HttpServlet {

Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
UserDao dao = new UserDaoImpl ();
// Obtain the client request parameters
String username = request. getParameter ("username ");
String password = request. getParameter ("password ");

User u = dao. login (username, password );
If (u! = Null ){
Out. print ("Logon successful ");
} Else {
Out. print ("null ");
}
Out. flush ();
Out. close ();
}
 

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 ();
}

} There are many tutorials on how to configure and use Tomcat and MySQL! Now we can implement the login module!

 

From the column chenlong12580

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.