import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class Example3 extends HttpServlet
{ 
	 boolean login_over = false;
	public void doGet (HttpServletRequest request, 
                           HttpServletResponse response) 
                                    throws ServletException, IOException {
		PrintWriter		out;
		String			title = "Example3 Simple Session Servlet";
		HttpSession session = request.getSession(true);

		response.setContentType("text/html");
		out = response.getWriter();
		out.println("<HTML><HEAD><TITLE>");
		out.println(title);
		out.println("</TITLE></HEAD><BODY>");
		out.println("<H1>Example3 Simple Session Servlet </H1>");

		long create_time = session.getCreationTime();
		long current_time = new Date().getTime();
		long diff = current_time - create_time; 

		if(session.isNew()) {
			 out.println("<H1><FONT COLOR=\"#ffoooo\">Welcome, Newcomer</FONT><H1>");
		} else if(diff >60*1000) {// Session is for one minute only
			 session.invalidate();
			 out.println("<H1><FONT COLOR=\"#ffoooo\">Please Login Again</FONT><H1>");
		} else if(login_over) {
			 out.println("Your session is alive no need to login again");
			 out.println("Your session details:");
			 out.println("<TABLE BORDER=2 ALIGN=CENTER>\n" +
                "<TR BGCOLOR=\"#FF8888\">\n" +
                "  <TH>Info Type<TH>Value\n" +
                "<TR>\n" +
                "  <TD>ID\n" +
                "  <TD>" + session.getId() + "\n" +
                "<TR>\n" +
                "  <TD>Creation Time\n" +
                "  <TD>" + new Date(session.getCreationTime()) + "\n" +
                "<TR>\n" +
                "  <TD>Time of Last Access\n" +
                "  <TD>" + new Date(session.getLastAccessedTime()) + "\n" +
                "<TR>\n" +
                "</TABLE>\n" );
			 out.println("</BODY></HTML>");
			 out.close();
		}
		out.println("Login name:<FONT COLOR=\"#ff0000\">example3</FONT><BR>");
		out.println("Password  :<FONT COLOR=\"#ff0000\">ex123</FONT>");
		out.println("<FORM ACTION=\"http://144.16.111.63:5050/servlet/Example3\" method =\"POST\">");
		out.println("<TABLE BORDER=0>");
		out.println("<TR>");
		out.println("<TD> <P>Enter Your Login </P> </TD>");
		out.println("<TD> <P><INPUT TYPE=text NAME=name SIZE=10></P> </TD>");
		out.println("</TR>");
		out.println("<TR>");
		out.println("<TD> <P>Enter Your Password</P> </TD>");
		out.println("<TD> <P><INPUT TYPE=password NAME=passwd SIZE=8 ></P> </TD>");
		out.println("</TR>");
		out.println("<TR>");
		out.println("<TD></TD><TD> <INPUT TYPE=\"submit\" VALUE=\"send\" > <INPUT TYPE=\"reset\" >");
		out.println("</TR>");
		out.println("</TABLE>");
		out.println("</FORM>");
		out.println("</BODY></HTML>");
		out.close();
	}

	public void doPost (HttpServletRequest request, 
                            HttpServletResponse response) 
                                      throws ServletException, IOException {
		PrintWriter		out;
		String			title = "Session Servlet Example3 ";
		String 			name = request.getParameter("name");
		String 			passwd = request.getParameter("passwd");
		HttpSession		session; 

		response.setContentType("text/html"); 
		out = response.getWriter();
		out.println("<HTML><HEAD><TITLE>");
		out.println(title);
		out.println("</TITLE></HEAD><BODY>");
		if(name.equals("example3") && passwd.equals("ex123")) {
			  login_over = true;
			  session = request.getSession(true);
			  out.println("<FONT COLOR=\"#ff0000\">Your session is created and it will be maintained for 1 minute. Session details are</FONT>"+
                "<TABLE BORDER=2 ALIGN=CENTER>\n" +
                "<TR BGCOLOR=\"#FF8888\">\n" +
                "  <TH>Info Type<TH>Value\n" +
                "</TR><TR>\n" +
                "  <TD>ID\n" +
                "  <TD>" + session.getId() + "\n" +
                "</TR><TR>\n" +
                "  <TD>Creation Time\n" +
                "  <TD>" + new Date(session.getCreationTime()) + "\n" +
                "<ession.getLastAccessedTimeTR>\n" +
                "</TABLE>\n" );
		} else {
			 out.println("<H1><FONT COLOR=\"#ff0000\">Invalid Login</FONT></H1>");
			 out.println("<a href=\"http://144.16.111.63:5050/servlet/Example3\">Try Again</a>");
		}
		out.println("</BODY></HTML>");
		out.println("</FORM>");
		out.close();
	}
}
