Android Tomcat 的應用之伺服器部分

來源:互聯網
上載者:User

接著昨天http://www.bkjia.com/kf/201203/124792.html 的寫,實現登入的服務端部分。首先得弄個資料庫,然後建立一個表,儲存所有使用者的使用者名稱和密碼,當在用戶端發出查詢請求的時候會把使用者輸入的使用者名稱和密碼傳到伺服器端,然後在資料庫中進行查詢,這裡我們的表就3個欄位,一個ID,一個username和一個password。

        然後就是編碼實現了,首先是寫一個類封裝一下資料庫中的使用者資訊,如下:

[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;
 }

}          然後就是定義查詢的介面:

[java] public interface UserDao { 
    // 登入方法  
    public User login(String username,String password); 
} 
public interface UserDao {
 // 登入方法
 public User login(String username,String password);
}          實現該介面:

[java] public class UserDaoImpl implements UserDao { 
    public User login(String account, String password) { 
        // 查詢SQL語句  
        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.executeQuery(); 
            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) {
  // 查詢SQL語句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.executeQuery();
   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;
 }

}         最後就是實現servlet,主要是處理來自用戶端的查詢請求和返回查詢結果:

[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(); 
        // 獲得用戶端請求參數  
        String username = request.getParameter("username"); 
        String password = request.getParameter("password"); 
         
        User u = dao.login(username, password); 
        if(u!=null){ 
            out.print("登入成功"); 
        }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();
  // 獲得用戶端請求參數
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  
  User u = dao.login(username, password);
  if(u!=null){
   out.print("登入成功");
  }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();
 }

}           至於Tomcat和MySQL的配置、使用網上有不少教程!至此就可以實現登入模組了!

 

摘自  chenlong12580的專欄 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.