Hi All,

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

Currently I am able to display all the questions on a single web page .

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.

Below is the code :-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = "jdbc:mysql://";
        String username1="root";
        String password1="root";
        PrintWriter out= response.getWriter();
            PreparedStatement st = null;
            ResultSet results = null;
            int limit=3;
            try {
                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);
                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("<BODY BGCOLOR='powderblue'></BODY>" +
                            "<FORM method=\"post\" action= \"Results.jsp\" >" +
                            "<TABLE BORDER = 2 CELLPADDING = 4 CELLSPACING = 2>" +
                            "<TR><TD COLSPAN = 6 ALIGN = CENTER><H2><B>Quick Quiz</B></H2></TD></TR>");
                    out.println("<TR><TD>" + r1 + "</TD></TR>");
                    out.println("<TR><TD><input type = \"radio\" name = \"answer\" id=\"input1\"  value = \"1\"  required> " + r2 + "</TD></TR><BR>");
                    out.println("<TR><TD><input type = \"radio\" name = \"answer\" id=\"input2\" value = \"2\">" + r3 + "</TD></TR><BR>");
                    out.println("<TR><TD><input type = \"radio\" name = \"answer\" id=\"input3\" value = \"3\">" + r4 + "</TD></TR><BR>");
                    out.println("<TR><TD><input type = \"radio\" name = \"answer\" id=\"input4\" value = \"4\">" + r5 + "</TD></TR><BR>");
                       session.setAttribute("correctanswer"+i, r6);
                  if (i <3)
                         out.println("<BR><BR><INPUT type = \"submit\" value = \"Press to Submit\"></form></body></html>" );
                   // session.setAttribute("selectedanswer1",(String)(request.getParameter("answer")) );  ---not working as expected          
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
            } catch (SQLException e) {
                // TODO Auto-generated catch block

Request you to help me in fetching the result of the quiz.

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 unable 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..

The required option which I have used is also not working properly.

Please help me in fetching the answer selected by user.