package Controllers;

import java.util.List;

import javax.persistence.EntityManager;

import flexjson.JSONSerializer;
import MVC.Controller;

/**
 * @author Alexander
 *
 */
public class UserCtl extends Controller {

    @Override
    public void doRequest(String actionName, Integer id) throws Exception {

        EntityManager em = this.getServletContext().getEM();

        // if(actionName.equals("create")) {
        //
        // User u = new User();
        //
        // // u.setEmail(getRequest().getParameter("email"));
        //
        // em.getTransaction().begin();
        // em.persist(u);
        // em.getTransaction().commit();

        //
        if (actionName.equals("get")) {
            List<?> list = em.createNamedQuery("User.findAll").getResultList();

            JSONSerializer serializer = new JSONSerializer();
            this.getRequest().setAttribute("json",
                    serializer.serialize(list));

            forward("/json.jsp");

        }

    }

}

@@ -84,16 +84,166 @@
 nager;%0D%0A
+import javax.persistence.NoResultException;%0D%0Aimport javax.persistence.Query;%0D%0A%0D%0Aimport toolbox.JSONResponse;%0D%0Aimport toolbox.Sha;%0D%0Aimport models.User;
 %0D%0Aimport
@@ -527,24 +527,83 @@
 ().getEM();%0D
+%0A        JSONSerializer serializer = new JSONSerializer();%0D
 %0A%0D%0A        /
@@ -1035,57 +1035,8 @@
     
-JSONSerializer serializer = new JSONSerializer();
 %0D%0A  
@@ -1184,16 +1184,1028 @@
       %7D%0D
+%0A        %0D%0A        if(actionName.equals(%22login%22))%0D%0A        %7B%0D%0A            JSONResponse response = null;%0D%0A            try%0D%0A            %7B%0D%0A                Query qry = em.createNamedQuery(%22User.findByEmailAndPassword%22);%0D%0A                qry.setParameter(%22email%22, this.getRequest().getParameter(%22email%22));%0D%0A                qry.setParameter(%22password%22, Sha.hash256(this.getRequest().getParameter(%22password%22)));%0D%0A                this.getRequest().getSession().setAttribute(%22user%22, (User) qry.getSingleResult());%0D%0A                response = new JSONResponse(true, null, null);%0D%0A            %7D%0D%0A            catch (NoResultException ex)%0D%0A            %7B%0D%0A                response = new JSONResponse(%22Invalid email address or password%22);%0D%0A            %7D%0D%0A            catch (Exception ex)%0D%0A            %7B%0D%0A                response = new JSONResponse(ex.getMessage());%0D%0A            %7D%0D%0A            %0D%0A            this.getRequest().setAttribute(%22json%22, serializer.serialize(response));%0D%0A            forward(%22/json.jsp%22);%0D%0A        %7D%0D
 %0A%0D%0A    %7D

Write login action for User controller