title: how to fetch the option selected by user while attempting the quiz made through servlets and jsp
body: <p>I am trying to develop an online quiz through Servlets and Jsp.
  I am using MYSQl as the backend database.
  The requirement is  to display few random questions to user.
  I have to store the results of the quiz in a separate table.
  The table should have the details such as username, question presented to each user ,whether it was correct or incorrect</p>
  
  <p>Currently I am able to display all the questions on a single web page  . </p>
  
  <p>But I am unable to fetch the answers selected by the user while attempting the quiz.
  I need to store each option selected by user and transfer it to a JSP page.</p>
  
  <p>Below  is the code :-</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  
          String url = "jdbc:mysql://127.0.0.1:3306/Quiz";
          String username1="root";
          String password1="root";
          PrintWriter out= response.getWriter();
  
              PreparedStatement st = null;
              ResultSet results = null;
  
              int limit=3;
              try {
  
                  Class.forName("com.mysql.jdbc.Driver");
                  Connection con= DriverManager.getConnection(url,username1,password1);
                  String sql="SELECT  Questions.Question, Questions.option1, Questions.option2,Questions.option3, Questions.option4,Questions.correctanswer FROM Questions order by RAND() limit ?";
                  st = (PreparedStatement) con.prepareStatement(sql);
                  st.setInt(1,limit);
                  results = st.executeQuery();
  
                  int i=1;
  
                      while (results.next())
                      {
                      String r1, r2, r3, r4, r5,r6;
                      r1 = results.getString(1);
                      r2 = results.getString(2);
                      r3 = results.getString(3);
                      r4 = results.getString(4);
                      r5 = results.getString(5);
                      r6 = results.getString(6);
                      out.println("&lt;BODY BGCOLOR='powderblue'&gt;&lt;/BODY&gt;" +
                              "&lt;FORM method=\"post\" action= \"Results.jsp\" &gt;" +
                              "&lt;TABLE BORDER = 2 CELLPADDING = 4 CELLSPACING = 2&gt;" +
                              "&lt;TR&gt;&lt;TD COLSPAN = 6 ALIGN = CENTER&gt;&lt;H2&gt;&lt;B&gt;Quick Quiz&lt;/B&gt;&lt;/H2&gt;&lt;/TD&gt;&lt;/TR&gt;");
                      out.println("&lt;TR&gt;&lt;TD&gt;" + r1 + "&lt;/TD&gt;&lt;/TR&gt;");
                      out.println("&lt;TR&gt;&lt;TD&gt;&lt;input type = \"radio\" name = \"answer\" id=\"input1\"  value = \"1\"  required&gt; " + r2 + "&lt;/TD&gt;&lt;/TR&gt;&lt;BR&gt;");
                      out.println("&lt;TR&gt;&lt;TD&gt;&lt;input type = \"radio\" name = \"answer\" id=\"input2\" value = \"2\"&gt;" + r3 + "&lt;/TD&gt;&lt;/TR&gt;&lt;BR&gt;");
                      out.println("&lt;TR&gt;&lt;TD&gt;&lt;input type = \"radio\" name = \"answer\" id=\"input3\" value = \"3\"&gt;" + r4 + "&lt;/TD&gt;&lt;/TR&gt;&lt;BR&gt;");
                      out.println("&lt;TR&gt;&lt;TD&gt;&lt;input type = \"radio\" name = \"answer\" id=\"input4\" value = \"4\"&gt;" + r5 + "&lt;/TD&gt;&lt;/TR&gt;&lt;BR&gt;");
                      out.println("&lt;/TABLE&gt;");
  
                      session.setAttribute("question"+i,r1);
                         session.setAttribute("correctanswer"+i, r6);
  
                    if (i &lt;3)
  
                          {
                        out.println("&lt;/FORM&gt;&lt;/BODY&gt;&lt;/HTML&gt;");
                          }
  
                       else
                          {
                           out.println("&lt;BR&gt;&lt;BR&gt;&lt;INPUT type = \"submit\" value = \"Press to Submit\"&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;" );
  
                          }   
                   i++;
  
                      }
  
                     // session.setAttribute("selectedanswer1",(String)(request.getParameter("answer")) );  ---not working as expected          
                      results.close();
                     st.close();
                      con.close();
  
              } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } 
  
  
              } 
  </code></pre>
  
  <p>Request you to help me in fetching the result of the quiz.</p>
  
  <p>I was also trying to display each questions in a separate web page and giving an option of "Next" button to move to the next question but I was unble to do that.
  So I tried other way and gave all the questions on single page .But I am unable to fetch the option selected by user while attempting the quiz..</p>
  
  <p>The required option which I have used is also not working properly.</p>
  
  <p>Please help me in fetching the answer selected by user. </p>
  
  <p>Thanks</p>