Servlet: simple session : Session « Servlets « Java






Servlet: simple session

  
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SimpleSession extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Simple Session Tracker</title>");
    out.println("</head>");
    out.println("<body>");

    out.println("<h2>Session Info</h2>");
    out.println("session Id: " + session.getId() + "<br><br>");
    out.println("The SESSION TIMEOUT period is "
        + session.getMaxInactiveInterval() + " seconds.<br><br>");
    out.println("Now changing it to 20 minutes.<br><br>");
    session.setMaxInactiveInterval(20 * 60);
    out.println("The SESSION TIMEOUT period is now "
        + session.getMaxInactiveInterval() + " seconds.");

    out.println("</body>");
    out.println("</html>");

  }

  /**
   * Handles the HTTP <code>POST</code> method.
   * 
   * @param request
   *            servlet request
   * @param response
   *            servlet response
   */
  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException,
      java.io.IOException {

    doGet(request, response);
  }

}

           
         
    
  








Related examples in the same category

1.Using Sessions in Servlet
2.Session Tracker
3.Session logger
4.Servlet: Session display
5.Servlet: session listener
6.Servlet : session filter
7.Servlet: session attribute listener
8.Servlet: Session bind listener
9.Servlet Session Example
10.Use cookie to save session data
11.Use hidden fields to save session data
12.Use URL rewrite to save session data
13.Session Events: implements HttpSessionBindingListener
14.Session Expiration Filter
15.Map adaptor for HttpSession objects
16.Fake session