Link to home
Start Free TrialLog in
Avatar of Drisya K S
Drisya K S

asked on

Random Quiz

This is my jsp page which display a quiz generated with random question.Now,my problem is i need to display the answr key of this quiz in the same order in which it is generated in the next jsp page and also compare the answer with the option chosen by the user.What should i do?????????
<html>
    <head>
    </head>
<body>
<%@page import="java.sql.*"%>

<form method="post" action="quizresult.jsp">
<table>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/recruitment","root","amma");
Statement st=connection.createStatement();
//ResultSet rs=st.executeQuery("Select * from question ");
ResultSet rs=st.executeQuery("SELECT * FROM question ORDER BY RAND() LIMIT 5");
int i=1;
while(rs.next()){
%>
<tr><td><%=i%></td>
    <td><%=rs.getString("question")%></td></tr>
  <tr>  <td><input type="radio" value="<%=rs.getString("opt1")%>" name="radio<%=i%>"/><%=rs.getString("opt1")%></td></tr>
  <tr>   <td><input type="radio" value="<%=rs.getString("opt2")%>" name="radio<%=i%>"/><%=rs.getString("opt2")%></td></tr>
  <tr>   <td><input type="radio" value="<%=rs.getString("opt3")%>" name="radio<%=i%>"/><%=rs.getString("opt3")%></td></tr>
  <tr>   <td><input type="radio" value="<%=rs.getString("opt4")%>" name="radio<%=i%>"/><%=rs.getString("opt4")%></td></tr>


<%
i++;
}
%>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

I'm sure you're done with this problem.

But in general, this is an example of holding info in the user session.  Or you can put the randomly ordered list of questions and answers into the html and pass it in the submit form -- but that would put the answers on your page.  Maybe only the questions with their id, and you could look up the answers in the page which displays after the submit?

If you choose to use the user session option, then in the same jsp page in which you generate the questions, create a collection of the questions in the same order, each with their answer, and save it to the session with something like
myses.setattribute("quizanswers",myAnswerCollection);

Then get the session attribute in the answer page so you know the answers in the right order.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.