title: Choose a color from a dropdown and store that color in a cookie.
body: <p>This is for school homework. I am stuck. I am to use servlets and create a dropdown which allows the user to choose a color, then change the background to reflect that color. Then anytime that cookie is current that backgound will be displayed that color. Here is the question from the homework for clarification sake:  You will need to build a Servlet that will check if a request has a cookie that contains the user's preferred color.  If the request does not have this cookie your code will create a HTML page with white background.  This page will be used to collect the user's color preference.  The page will contain a drop down selection box with these four options:  Select, White, Blue, and Gray.  Use JavaScript to ensure that the user have selected something other than the "Select" entry.  Javascript should be implemented with Event Listeners and not in-line.  Do not allow submission if the user did not make a valid selection.  Once the user have made a selection and submit the page, your code will store the user's color preference in a cookie and generate a page setting the background to the color that was previously selected by the user.  The cookie is to only be valid for 30 seconds.  Note: Once the cookie has been created, navigating back to this Servlet (without closing the browser) should render the correct background without requesting that the user supply the information again.  After the cookie has expired, the Servlet should once again request the user for color preference.</p>
  
  <p>Here is my Cookie Serlevt:</p>
  
  <pre><code>package edu.edgewood.servlet;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   * Servlet implementation class CookieServlet
   */
  @WebServlet("/CookieServlet")
  public class CookieServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
      protected final static String PREFS_BGCOLOR = "prefs.bgcolor";
      protected String faveColor = DEFAULT_COLOR;
      protected final static String DEFAULT_COLOR = "white";
  
      public CookieServlet() {
          super();
      }
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
       *      response)
       */
      @SuppressWarnings("unused")
      protected void doGet(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
  
          response.setContentType("text/html");
          final String PREFS_BGCOLOR = "prefs.bgcolor";
          PrintWriter printWriter = response.getWriter();
  
          Cookie[] cookie = request.getCookies();
  
          if (cookie == null) {
              printWriter.println("No Cookies");
              printWriter.println("&lt;/br&gt;");
              renderPage(printWriter, false);
  
          } else {
  
              for (int i = 0; i &lt; cookie.length; i++) {
                  Cookie cookies = cookie[i];
                  if (cookies.getName().equals(PREFS_BGCOLOR)) {
                      faveColor = cookies.getValue();
                      printWriter.println("This is your colored page"); //testing
                      //printWriter.println("&lt;body bgcolor= 'PREFS_BGCOLOR'&gt;"); //testing
                      printWriter.println("&lt;body bgcolor=\"");
                      printWriter.print(faveColor);
                      printWriter.println("\"&gt;");
                      break;
                  }
  
                  printWriter.println(cookies.getValue());
              }
          }
      }
  
      private void renderPage(PrintWriter printWriter, boolean post) {
          printWriter.println("&lt;html&gt;");
          printWriter.println("&lt;head &gt;");
          printWriter.println("&lt;script language='javascript' src='js/common.js'&gt;&lt;/script&gt;");
          printWriter.println("&lt;/head&gt;");
          printWriter.println("&lt;body bgcolor= 'faveColor'&gt;"); //determines background
          printWriter.println("Please choose a color");
          printWriter.println("&lt;form action='CookieServlet' method='post'&gt;"); 
          printWriter.println("&lt;select id='colorid' onchange='if(this.value == '') this.selectedIndex = 1' name ='colorid'&gt;&lt;option value='' disabled='disabled' selected='selected'&gt;Please select a color&lt;/option&gt; &lt;option value='#FF0000'&gt;Red&lt;/option&gt; &lt;option value='#0000FF'&gt;Blue&lt;/option&gt; &lt;option value='#00FF00'&gt;Green&lt;/option&gt;");
          printWriter.println("&lt;input type='button' id='submitBtn' value='Submit' onClick=getElementById('colorid').value/&gt;");
          printWriter.println("&lt;/select&gt;");
          printWriter.println("&lt;/br&gt;");
          printWriter.println("&lt;/form&gt;");
          printWriter.println("&lt;script language ='javascript'&gt;addControlListeners();&lt;/script&gt;");
          printWriter.println("&lt;/body&gt;");
          printWriter.println("&lt;/html&gt;");
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
       *      response)
       */
      protected void doPost(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
  
          Cookie cookie = new Cookie(PREFS_BGCOLOR, faveColor);
          cookie.setMaxAge(10);
          response.addCookie(cookie);
      }
  
  }
  </code></pre>
  
  <p>Currently, the initial page is rendering yellow... not sure where that comes from. Then I choose a color in the dropdown and click submit. The submit button is not validating but then goes to a blank white screen. Then I go back to the page when the cookie is still active and the page is rendering white with my text displaying. Any help is greatly appreciated. Thank you. </p>