title: JSP sample choice quiz
body: <p>I am trying to create a multiple choice html page and need to use jsp to view the results.  However, I can't seem to figure out how to get a jsp file to get the final score results after hitting the submit button. (for example, you got _ out of 4 questions correct _%)  I can set up the html file just fine.  What am I doing wrong?  Thanks.</p>
  
  <p>below is an example page with the html code "SampleQuiz.html"</p>
  
  <h1><br/></h1>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Multiple-Choice Quiz Example&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;h2&gt;Sample Multiple-Choice Quiz&lt;/h2&gt;
  
  &lt;form action="quizresults.jsp" method="get"&gt;
  
  &lt;h3&gt;Q1. What color is the sky on a sunny day?&lt;/h3&gt;
  &lt;input type="radio" name="answer1" value="blue" checked="checked"&gt;blue&lt;br&gt;
  &lt;input type="radio" name="answer1" value="red"&gt;red&lt;br&gt;
  &lt;input type="radio" name="answer1" value="green"&gt;green&lt;br&gt;
  
  &lt;h3&gt;Q2. The sum of two and three equals...&lt;/h3&gt;
  &lt;input type="radio" name="answer2" value="seven"&gt;seven&lt;br&gt;
  &lt;input type="radio" name="answer2" value="six"&gt;six&lt;br&gt;
  &lt;input type="radio" name="answer2" value="five" checked="checked" &gt;five&lt;br&gt;
  
  &lt;h3&gt;Q3. Which of these animals is NOT an mammal.&lt;/h3&gt;
  &lt;input type="radio" name="answer3" value="lizard" checked="checked"&gt;lizard&lt;br&gt;
  &lt;input type="radio" name="answer3" value="monkey"&gt;monkey&lt;br&gt;
  &lt;input type="radio" name="answer3" value="dog"&gt;dog&lt;br&gt;
  
  &lt;h3&gt;Q4. Santa Claus is most associated with this holiday.&lt;/h3&gt;
  &lt;input type="radio" name="answer4" value="Halloween"&gt;Halloween&lt;br&gt;
  &lt;input type="radio" name="answer4" value="Christmas"checked="checked"&gt;Christmas&lt;br&gt;
  &lt;input type="radio" name="answer4" value="Fourth of July"&gt;Forth of July&lt;br&gt;
  &lt;br&gt;
  &lt;input type="submit" value="Submit"&gt;
  &lt;input type="reset" value="Reset"&gt;
  
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p>Update:  here's my second attempt at jsp.  I also changed the html to the below answer's suggestion.</p>
  
  <p><br/></p>
  
  <p>below is my attempted jsp code "quizresults.jsp"
  <br/></p>
  
  <pre><code>&lt;html&gt;
  
    &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
    &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd"&gt;
  
          &lt;html&gt;
          &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;Final Quiz Score&lt;/title&gt;
     &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Here are your results&lt;/h1&gt;
  
         &lt;%
                 if(request.getParameter("answer") != null) {
                     if(request.getParameter("answer").equals("answer1")) {
                         out.println("Question 1 was correct&lt;br&gt;");
                  }
                  else {
                      out.println("Question 1 was incorrect.  Try again.&lt;br&gt;");
                  }
                  if(request.getParameter("answer").equals("answer2")) {
                      out.println("Question 2 was answered.&lt;br&gt;");
                  }
                  else {
                      out.println("Question 2 was incorrect.  Try again.&lt;br&gt;");
                  }
                  if(request.getParameter("answer").equals("answer3")) {
                      out.println("Question 3 was answered.&lt;br&gt;");
                  }
                  else {
                      out.println("Question 3 was incorrect.  Try again.&lt;br&gt;");
                  }
                  if(request.getParameter("answer").equals("answer4")) {
                      out.println("Question 4 was correct.&lt;br&gt;");
                  }
                  else {
                      out.println("Question 4 was incorrect.  Try again.&lt;br&gt;");
                  }
              }
      %&gt;
  
       &lt;form action="SampleQuiz.html" method="post"&gt;
      &lt;input type="submit" value="Return"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>