/*
 * @(#)Binder.java
 *
 * Copyright (c) 1998 Karl Moss. All Rights Reserved.
 *
 * You may study, use, modify, and distribute this software for any
 * purpose provided that this copyright notice appears in all copies.
 *
 * This software is provided WITHOUT WARRANTY either expressed or
 * implied.
 *
 * @author  Karl Moss
 * @version 1.0
 * @date    20Jan99
 *
 */

// package javaservlets.session;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * <p>This is a simple servlet that binds an object that
 * implements the HttpSessionBindingListener interface.
 */

public class Binder extends HttpServlet
{
  /**
    * <p>Performs the HTTP GET operation
    *
    * @param req The request from the client
    * @param resp The response from the servlet
    */

  public void doGet(HttpServletRequest req,
                    HttpServletResponse resp)
    throws ServletException, java.io.IOException
    {
      // Set the content type of the response
      resp.setContentType("text/html");

      // Get the PrintWriter to write the response
      java.io.PrintWriter out = resp.getWriter();

      // Get the session object for this client session.
      // The parameter indicates to create a session
      // object if one does not exist
      HttpSession session = req.getSession(true);

      // Create a new SessionObject
      SessionObject o = new SessionObject();

      // Put the new SessionObject into the session


      session.putValue("Binder.object", o);

      // Print a standard header
      out.println("<html>");
      out.println("<head>");
      out.println("<title>Session Binder</title>");
      out.println("</head>");
      out.println("<body>");
      out.println("Object bound to session " +
                  session.getId());

      // Wrap up
      out.println("</body>");
      out.println("</html>");
      out.flush();
    }

  /**
    * <p>Initialize the servlet. This is called once when the
    * servlet is loaded. It is guaranteed to complete before any
    * requests are made to the servlet
    *
    * @param cfg Servlet configuration information
    */

  public void init(ServletConfig cfg)
    throws ServletException
    {
      System.out.println("Binder init by us");
      super.init(cfg);
    }

  /**
    * <p>Destroy the servlet. This is called once when the servlet
    * is unloaded.
    */

  public void destroy()
    {
          System.out.println("Binder destroy by us ");
          super.destroy();
    }

}

