Thứ Hai, 21 tháng 7, 2014

Login Using JSF

Đầu tiên ta tạo một project và chọn framework JavaServer Faces.
Sau đó tạo ra hai trang JSF là index.xhtml và home.xhtml. Tiếp tục ta viết vào trogn trang index.xhtml như dưới đây.


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:inputText value="#{login.username}" required="" requiredMessage="Bạn phải nhập username"></h:inputText><br></br>
            <h:inputSecret value="#{login.password}" required="" requiredMessage="Bạn phải nhập password"></h:inputSecret><br></br>
            <h:commandButton action="#{login.checkLogin}" value="Login"/>
        </h:form>
    </h:body>
</html>

Tiếp đó để có thể kết nối vào cơ sở dữ liệu ta sẽ tạo một lớp DataAccess như dưới:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package luj.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nguyen Minh An
 */
public class DataAccess {
    public static Connection getConnection() throws SQLException {
        String url = "jdbc:sqlserver://localhost:1433;databaseName=LOGIN_DEMO";
        String username = "LUCKY";
        String password = "123456";

        try {
            Connection conn = null;
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(url, username, password);
            return conn;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}

Tiếp theo ta tạo tiếp một lớp Java với tên là AccountManagement để xử lý nghiệp vụ như sau:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package luj.bussiness;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import luj.db.DataAccess;

/**
 *
 * @author Nguyen Minh An
 */
public class AccountManagement {

    public boolean checkLogin(String username, String password) {
        if ("".equals(username) || "".equals(password)) {
            return false;
        } else {
            try {
                Connection conn = DataAccess.getConnection();
                PreparedStatement ps = conn.prepareCall("SELECT * FROM ACCOUNT WHERE username like '" + username + "' and passwords like '" + password + "'");
                ResultSet res = ps.executeQuery();
                while (res.next()) {
                    return true;
                }
            } catch (SQLException ex) {
                Logger.getLogger(AccountManagement.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }
}

Và cuối cùng ta tạo một lớp JSF Managed Bean với tên là Login như dưới đây:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package luj.task;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import luj.bussiness.AccountManagement;

/**
 *
 * @author Nguyen Minh An
 */
@ManagedBean
@RequestScoped
public class Login {
    private String username;
    private String password;
    
    public Login() {
    }
    
    public String checkLogin(){
        boolean res = new AccountManagement().checkLogin(username, password);
        if (res == true) {
            return "home?faces-redirect=true";
        }else{
            return "index?faces-redirect=true";
        }
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    
    
}


Code dễ hiểu, dễ làm, nhanh, tuy nhiên độ trong suốt không cao.


Không có nhận xét nào:

Đăng nhận xét