Login with EJB sessionbean and servlet

Datetime:2016-08-23 01:58:12          Topic: EJB           Share

I am trying to build a login stateless session bean that i will be calling from my servlet.The query selects ACCESS(boolean value) from the user table and if the value is true access will be granted. if false it means account is deactivated and if the resultlist is empty user shout get failed login. am just having trouble implementing it.

package Dao;
import Model.Account;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class AccountAccessDao implements AccountAccessDaoLocal {
	boolean status=false;
	@PersistenceContext
	private EntityManager emlogin;
	@Override
	public boolean accountLogin(String username,String password) {
	Query query = emlogin.createNativeQuery("SELECT e.access FROM Account e WHERE e.username='" + username + "'AND e.password='" + password + "'",Account.class);
	return query.getResultList().contains(this);
}
}
package Controller;
import Dao.AccountAccessDaoLocal;
import java.io.IOException;
import static java.lang.System.out;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AccessServlet extends HttpServlet {
@EJB
	private AccountAccessDaoLocal userlogin;
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			boolean status = userlogin.accountLogin(username, password);
			if(status==true){
				out.println("Success");
				//Accout confirmation page
			}else if(status==false){
				out.println("account locked");
				//Accout confirmation page
			}else {
				out.println("Failure");
				//failure page here
			}
 } }





About List