package com.windofkeltia.wtp.tutorial;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

/**
 * Tutorial: Using the Eclipse Web Tools Platform with Apache Tomcat
 * Copyright (c) 2008 by Russell Bateman and Etretat Logiciels, LLC.
 * Permission is granted for any non-commercial, derivational use. You are
 * forbidden only to repost the files of the tutorial and project without
 * express permission of the author.
 *
 * This is the form that starts the whole thing off in the second tutorial
 * example.
 *
 * @author russ@windofkeltia.com
 */
public class GreetingAndColorServlet extends javax.servlet.http.HttpServlet
  implements javax.servlet.Servlet
{
  static final long serialVersionUID = 1L;

  public GreetingAndColorServlet()
  {
    super();
  }

  protected void doGet( HttpServletRequest  request,
                        HttpServletResponse response )
    throws ServletException, IOException
  {
    int       which = 0;
    String    greeting = request.getParameter( "greeting" );
    String    color    = request.getParameter( "color" );
    String[]  schemes  = {"#FFC6A5", // red
                          "#F7FFCE", // green
                          "#BDC6DE", // blue
                          "#FFFFC6"  // yellow
                          };

    /* This servlet processes a GET or POST coming from greet-color.html. The
     * filled-in values come to us as request parameters.
     *
     * In reading a request parameter, you need to check for the value being
     * a) nil, b) an empty string and c) a nonempty string, but one of the
     * wrong format. Here, we're looking for a greeting (name we gave in the
     * HTML form), but for (c), we don't care because it's the user's greeting.
     */
    if(  greeting == null
      || greeting.trim().equals( "" )
      || false )
    {
      // do whatever for a failed greeting here...
    }

    /* Getting the value of a set of radio buttons is shown here. What defines
     * a set is that they all share the same name (in HTML). You still have to
     * validate as recommended above just in case although in theory you'll not
     * get a bad value back since it's not under user-control.
     */
    if(     color != null
        && !color.trim().equals( "" )
       && ( color.equals( "0" ) || color.equals( "1" ) || color.equals( "2" ) || color.equals( "3" ) ) )
    {
      /* Convert color to an index here because we're not really going to have
       * red, blue or green as backgrounds--that would be way too ugly.
       */
      which = Integer.parseInt( color.trim() );
    }

    request.setAttribute( "bgcolor",  schemes[ which ] );
    request.setAttribute( "greeting", greeting );

    RequestDispatcher  view = request.getRequestDispatcher( "/color.jsp" );
    view.forward( request, response );
  }

  protected void doPost( HttpServletRequest  request,
                         HttpServletResponse response )
    throws ServletException, IOException
  {
    doGet( request, response );
  }
}
