001package edu.pdx.cs410J.servlets;
002
003import javax.servlet.http.*;
004import javax.servlet.ServletException;
005import java.io.IOException;
006import java.io.PrintWriter;
007import java.util.Date;
008import java.util.Enumeration;
009
010/**
011 * A servlet that uses a session to maintain information about a user
012 */
013public class SessionServlet extends HttpServlet {
014  private static final String VISIT_COUNT = "VISIT_COUNT";
015
016  /**
017   * Generates HTML that summarizes the session
018   */
019  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
020    HttpSession session = request.getSession(false);
021    if (session == null) {
022      session = request.getSession(true);
023      session.setAttribute(VISIT_COUNT, 0);
024    }
025
026    int visits = (Integer) session.getAttribute(VISIT_COUNT);
027    visits++;
028    session.setAttribute(VISIT_COUNT, visits);
029
030    response.setContentType("text/html");
031    PrintWriter out = response.getWriter();
032    dumpHeader(out);
033
034    out.println("<h1>You have visited this page " + visits + " times</h1>");
035
036    dumpSession(session, out);
037    dumpCoookies(request.getCookies(), out);
038
039    out.println("<h2>End the session</h2>");
040
041    out.println("<form action='session' method='post'>");
042    out.println("<input type='submit' value='End Session'/>");
043    out.println("</form>");
044
045    dumpFooter(out);
046  }
047
048  private void dumpFooter(PrintWriter out) {
049    out.println("</body>");
050    out.println("</html>");
051  }
052
053  private void dumpHeader(PrintWriter out) {
054    out.println("<html>");
055    out.println("<head><title>Session Demo</title></head>");
056    out.println("<body>");
057  }
058
059  /**
060   * Ends the session
061   */
062  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
063    HttpSession session = request.getSession(false);
064    if (session != null) {
065      session.invalidate();
066    }
067
068    response.setContentType("text/html");
069    PrintWriter out = response.getWriter();
070    dumpHeader(out);
071
072    out.println("<h1>Your session has ended</h1>");
073
074    dumpCoookies(request.getCookies(), out);
075
076    out.println("<h2>Return to Session</h2>");
077    out.println("<form action='session' method='get'>");
078    out.println("<input type='submit' value='Go Back'/>");
079    out.println("</form>");
080
081    dumpFooter(out);
082  }
083
084  private void dumpCoookies(Cookie[] cookies, PrintWriter out) {
085    out.println("<h2>Your cookies:</h2>");
086    for (Cookie cookie : cookies) {
087      dumpKeyValue("Name", cookie.getName(), out);
088      dumpKeyValue("Domain", cookie.getDomain(), out);
089      dumpKeyValue("Path", cookie.getPath(), out);
090      dumpKeyValue("Value", cookie.getValue(), out);
091      dumpKeyValue("Comment", cookie.getComment(), out);
092      dumpKeyValue("Version", cookie.getVersion(), out);
093      dumpKeyValue("Max Age", cookie.getMaxAge(), out);
094      dumpKeyValue("Secure", cookie.getSecure(), out);
095    }
096  }
097
098  private void dumpSession(HttpSession session, PrintWriter out) {
099    out.println("<h2>Your session:</h2>");
100    dumpKeyValue("Id", session.getId(), out);
101    dumpKeyValue("Creation Time" , new Date(session.getCreationTime()), out);
102    dumpKeyValue("Last Accessed Time", new Date(session.getLastAccessedTime()), out);
103    dumpKeyValue("Max inactive interval", session.getMaxInactiveInterval(), out);
104    for (Enumeration e = session.getAttributeNames(); e.hasMoreElements(); ) {
105      String name = (String) e.nextElement();
106      dumpKeyValue("Attribute \"" + name + "\"", session.getAttribute(name), out);
107    }
108  }
109
110  private void dumpKeyValue(String name, Object value, PrintWriter out) {
111    out.println("<p><b>" + name + "</b>: " + value + "</p>");
112  }
113}