@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
 public String authenticate(ModelMap model, HttpServletRequest request) {
   String email = HtmlUtils.htmlEscape(request.getParameter("email"));
   String password = HtmlUtils.htmlEscape(request.getParameter("password"));
   String p = HtmlUtils.htmlEscape(request.getParameter("p"));
   PersistenceManager pm = PMF.get().getPersistenceManager();
   Query q = pm.newQuery(Category.class);
   List<Category> result = null;
   model.addAttribute("subCategoryList", categoryDao.getSubCategoryList());
   try {
     result = (List<Category>) q.execute();
     if (result.isEmpty()) {
       model.addAttribute("listCategory", null);
     } else {
       model.addAttribute("listCategory", result);
     }
     pm = PMF.get().getPersistenceManager();
     Query q1 = null;
     q = pm.newQuery(User.class);
     q.setFilter("email == emailParam && password == passwordParam");
     // q.setOrdering("date desc");
     q.declareParameters("String emailParam,String passwordParam");
     List<User> results = (List<User>) q.execute(email, password);
     // System.out.println(email + " " + password + results.size());
     if (results.size() >= 1) {
       HttpSession hs = request.getSession(true);
       hs.setAttribute("userid", email);
       hs.setAttribute("username", results.get(0).getUserName());
       hs.setAttribute("collegeName", results.get(0).getCollege());
       hs.setAttribute("contactNo", results.get(0).getMobile());
       model.addAttribute("productDao", productDao);
       model.addAttribute("result", "Login Successfully!");
       if (p != null && (!p.equals("null")))
         // return new ModelAndView("redirect:"+p);
         return p;
       else return "index";
     } else {
       model.addAttribute("result", "Incorrect User ID or Password! Try Again");
       model.addAttribute("p", p);
       return "login";
     }
   } catch (Exception e) {
     e.printStackTrace();
     // System.out.println("in exception");
     model.addAttribute("result", "Incorrect Userid or Password! Try Again");
     return "login";
   } finally {
     q.closeAll();
     pm.close();
   }
 }
 @RequestMapping(value = "/resetforgotpassword", method = RequestMethod.POST)
 public String resetforgotPassword(HttpServletRequest request, ModelMap model) {
   String email = HtmlUtils.htmlEscape(request.getParameter("email"));
   if (email != null) {
     String userid = email;
     String password = HtmlUtils.htmlEscape(request.getParameter("password"));
     PersistenceManager pm = PMF.get().getPersistenceManager();
     Query q = pm.newQuery(User.class);
     q.setFilter("email==userid");
     q.declareParameters("String userid");
     List<User> result = (List<User>) q.execute(userid);
     try {
       User c = pm.getObjectById(User.class, result.get(0).getUserId());
       c.setPassword(password);
       model.addAttribute("registered", "Password reset successfully, Login now !!");
       model.addAttribute("subCategoryList", categoryDao.getSubCategoryList());
     } finally {
       pm.close();
     }
     return "login";
   } else {
     model.addAttribute("subCategoryList", categoryDao.getSubCategoryList());
     return "login";
   }
 }
 @RequestMapping(value = "/resetpassword", method = RequestMethod.POST)
 public ModelAndView resetPassword(HttpServletRequest request, ModelMap model) {
   HttpSession hs = request.getSession(false);
   if (hs != null) {
     String userid = (String) hs.getAttribute("userid");
     String password = HtmlUtils.htmlEscape(request.getParameter("password"));
     PersistenceManager pm = PMF.get().getPersistenceManager();
     Query q = pm.newQuery(User.class);
     q.setFilter("email==userid");
     q.declareParameters("String userid");
     List<User> result = (List<User>) q.execute(userid);
     try {
       User c = pm.getObjectById(User.class, result.get(0).getUserId());
       c.setPassword(password);
       model.addAttribute("resultresetpage", "Password reset successfully");
     } finally {
       pm.close();
     }
     return new ModelAndView("redirect:resetpassword");
   } else return new ModelAndView("redirect:resetpassword");
 }