public class LoginServlet extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		String n = request.getParameter("user_name");
		String p = request.getParameter("user_password");

		boolean authenticUser = false;
		boolean newLoginRequest = false;

		HttpSession session = request.getSession();

		if (session != null && session.getAttribute("sessionId") != null)
		{
			authenticUser = true;
			newLoginRequest = false;
		}
		else
		{
			DataSource ds = DBUtils.getInstance().getUserDS();
			try
			{
				Map ls = ds.fetchSingle("name", n);
				if (ls != null)
				{
					if (n != null && p != null)
					{
						String desc = ls.get("description").toString();
						String desc1 = HashUtils.sha(ls.get("name").toString());
						if (n.equals(ls.get("name")) && p.equals(ls.get("password")) && desc.equals(desc1) )
						{
							authenticUser = true;
							newLoginRequest = true;
						}
						else
						{
							authenticUser = false;
						}
					}
				}

			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		// Credential are correct!!
		if (authenticUser)
		{
			if (newLoginRequest)
			{
				session.setAttribute("sessionId", request.getSession().getId());
				response.sendRedirect(ConfigUtils.getInstance().getLandingPageURL());
			}
		}
		else
		{
			request.getSession().invalidate();
			response.sendRedirect(ConfigUtils.getInstance().getLoginURL());
		}

		out.close();
	}
}