title: JSP - Get score from a jsp quiz application
body: <p>Hi I'm creating a quiz application in jsp and am using different jsp pages for every question. I'd like to keep a score after each question is answered. My problem is that I'm picking random answers from a database, within which lies the correct answer too. Since I can't guess which letter the correct answer will appear on, could you suggest what I should do? I am posting the first and second question jsp's to give you an idea of how I'm doing it:</p>
  
  <p>q1.jsp</p>
  
  <pre><code>&lt;%@page import="java.util.Random"%&gt;
  &lt;%@page import="java.util.ArrayList"%&gt;
  &lt;%@page import="org.me.jsp.beans.WordBean"%&gt;
  &lt;%@page import="java.util.List"%&gt;
  &lt;!--This JSP acts as the first question in a multiple choice quiz, with the user
  asked to submit their answer to the question--&gt;
  
  &lt;?xml version = "1.0"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&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;jsp:useBean id = "wordData" scope = "request"
               class = "org.me.jsp.beans.WordDataBean" /&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Big Java Quiz, question 1&lt;/title&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;%if (request.getParameter("choice").equals("N")) {
                  out.print("&lt;meta http-equiv='refresh' content='0;url=options.jsp'/&gt;");
              }
              //Redirects user back to index if they did not want to take quiz%&gt;
          &lt;form action="q2.jsp" method="POST"&gt;
              &lt;%
                  List&lt;WordBean&gt; wordList = wordData.getWordList();
                  List&lt;String&gt; answersList = new ArrayList&lt;String&gt;();
                  Random random = new Random();
                  Random forAnswers = new Random();
  
                  WordBean goodOne = wordList.get(random.nextInt(wordList.size()));
                  //take it out from the list
                  wordList.remove(goodOne);
                  //add it to the answers list
                  answersList.add(goodOne.getGermanName());
                  WordBean fakeOne = wordList.get(random.nextInt(wordList.size()));
                  //take it out from the list
                  wordList.remove(fakeOne);
                  //add it to the answers list
                  answersList.add(fakeOne.getGermanName());
                  WordBean fakeTwo = wordList.get(random.nextInt(wordList.size()));
                  //take it out from the list
                  wordList.remove(fakeTwo);
                  //add it to the answers list
                  answersList.add(fakeTwo.getGermanName());
  
              %&gt;What is the English word for the German word &lt;%=goodOne.getEnglishName()%&gt;?&lt;br&gt;
  
                  &lt;%
                      char letter = 'A';
                      for (String answer : answersList) {
                  %&gt;
                  &lt;input type="radio" name="q1Answer" value=""/&gt;&lt;label for="&lt;%=letter%&gt;"&gt;&lt;%=letter%&gt;)&lt;%=answersList.get(forAnswers.nextInt(3))%&gt;  /&gt; 
                      &lt;% //point to the next letter
                              letter++;
                          }
                          %&gt;
                      &lt;input type="submit" value="Submit"/&gt;
                      &lt;/form&gt;
                      &lt;/body&gt;
                      &lt;/html&gt;
  </code></pre>
  
  <p>q2.jsp</p>
  
  <pre><code>&lt;%-- 
      Document   : q2
      Created on : 06-May-2012, 18:54:32
      Author     : encore
  --%&gt;
  
  &lt;!--This JSP acts as the second question in a multiple choice quiz, with the user
  asked to submit their answer to the question--&gt;
  
  &lt;?xml version = "1.0"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&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;title&gt;Big Java Quiz, question 2&lt;/title&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;%int score = 0;
          if(request.getParameter("q1Answer").equals("C"))
              score++; //Increments score if answer submitted was correct%&gt;
          &lt;form action="q3.jsp" method="POST"&gt;
              Your current score is: &lt;%out.print(score);%&gt;/20
              &lt;input type="hidden" name="q2Score" value="&lt;%out.print(score);%&gt;"/&gt;
              &lt;!--Hidden button allows score to be accessed by next JSP--&gt;
              &lt;b&gt;Question 2.&lt;/b&gt; When an exception is generated it is said to have been _________?&lt;br/&gt;&lt;br/&gt;
              &lt;input type="radio" name="q2Answer" value="A"/&gt;&lt;label for="A"&gt;A) Built&lt;/label&gt;&lt;br/&gt;
              &lt;input type="radio" name="q2Answer" value="B"/&gt;&lt;label for="B"&gt;B) Thrown&lt;/label&gt;&lt;br/&gt;
              &lt;input type="radio" name="q2Answer" value="C"/&gt;&lt;label for="C"&gt;C) Caught&lt;/label&gt;&lt;br/&gt;
              &lt;input type="radio" name="q2Answer" value="D"/&gt;&lt;label for="D"&gt;D) Detected&lt;/label&gt;&lt;br/&gt;&lt;br/&gt;
              &lt;input type="submit" value="Submit"/&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Note: the form of the second question will obviously be changed to something else, currently I'm concentrating on getting the score .</p>