import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/*
    This servlet is used only to handle requests to
    empty the contents of the Shopping Basket.
    Once the basket has been emptied, the servlet redirects
    the browser to the WelcomeServlet.
*/
public class EmptyBasketServlet extends HttpServlet {

  // Method Name: init()
  // Purpose: This is the default init() method.
  public void init(ServletConfig config)
    throws ServletException {

    super.init(config);
  }

  // Method Name: emptyBasket()
  // Purpose: This method gets a reference to the basket
  // Vector and calls the removeAllElements method, emptying
  // the entire contents of the shopping basket.
  private void emptyBasket(HttpServletRequest request) {

    // Get/Create a reference to the HttpSession object.
    HttpSession session = request.getSession(true);

    if ( session != null ) {

      // Get a reference to the basket Vector.
      Vector basket = (Vector)session.getValue("basket");

      // If basket is null, create one
      if ( basket != null ) {

        // Empty the basket.
        basket.removeAllElements();
      }
    }
  }

  // Method Name: doGet()
  // Purpose: Services requests to empty the current
  // contents of the shopping basket.  It then redirects
  // the browser to the WelcomeServlet.
  public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    emptyBasket(request);
    response.sendRedirect("/servlet/WelcomeServlet");
  }

  //Get Servlet information
  public String getServletInfo() {

    return "EmptyBasketServlet Information";
  }
}
