String outStr = new String();
boolean success = false;
SessionManager sm = new SessionManager(request.getSession());
if(silentRedirect!=null){
/** Redirected from another page. Redirect back to that page instead of displaying this page **/
if(logout!=null){
sm.logOut();
outStr = ("Logout successful");
} else
outStr = sm.logIn("",passwd).getMessage();
sm.setServerMessage(outStr);
/** Silently redirect after processing **/
response.sendRedirect(silentRedirect);
} else {
if(sm.isAuthenticated()&&logout==null)
outStr = "You are already logged in. If you wish to logout, please press <a href=\"login?logout=true\">here</a>.";
else if(logout!=null){
sm.logOut();
outStr = "Logout successful!";
if(redirect!=null)
response.sendRedirect(redirect);
} else if(passwd!=null){
/** Password supplied, proceed with login **/
LoginResult lr = sm.logIn("",passwd);
success = lr.isSuccess();
outStr = lr.getMessage();
if(success && redirect!=null)
response.sendRedirect(redirect);
}
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet login</title>");
out.println("</head>");
out.println("<body>");
if(sm.isAuthenticated())
out.println("<a href=\"admin\">back</a><br/>");
else
out.println("<a href=\"page\">back</a><br/>");
out.println("<h3>" + outStr + "</h3>");
if(!sm.isAuthenticated()){
out.println("<b>Please enter your password below:</b>");
out.println("<form action=\"login\" method=\"POST\">");
out.println("Password: <input type=\"text\" name=\"password\"/><br/>");;
out.println("<input type=\"submit\" name\"log in\"/>");
out.println("</form>");