Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to fetch the option selected by user while attempting the quiz made through servlets and jsp

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Thanked 0 Times in 0 Posts

    Default how to fetch the option selected by user while attempting the quiz made through servlets and jsp

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,455 Times in 2,406 Posts

    Default Re: how to fetch the option selected by user while attempting the quiz made through servlets and jsp

    Also posted at: How To Fetch The Option Selected By User While Attempting The Quiz Mad - JavaEE/J2EE | Dream.In.Code
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help about option select in jsp page
    By langtu_hqb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 6th, 2013, 12:04 PM
  2. dynamically fetch values from database and display in combo box in jsp
    By Ritika Rishi in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: November 29th, 2013, 10:28 AM
  3. changes made to JSP or servlets
    By the light in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: May 30th, 2011, 08:19 AM