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.j dbc.Driver ").newInst ance();
Connection connection = DriverManager.getConnectio n("jdbc:my sql://loca lhost/recr uitment"," root","amm a");
Statement st=connection.createStatem ent();
//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("quest ion")%></t d></tr>
<tr> <td><input type="radio" value="<%=rs.getString("op t1")%>" name="radio<%=i%>"/><%=rs. getString( "opt1")%>< /td></tr>
<tr> <td><input type="radio" value="<%=rs.getString("op t2")%>" name="radio<%=i%>"/><%=rs. getString( "opt2")%>< /td></tr>
<tr> <td><input type="radio" value="<%=rs.getString("op t3")%>" name="radio<%=i%>"/><%=rs. getString( "opt3")%>< /td></tr>
<tr> <td><input type="radio" value="<%=rs.getString("op t4")%>" name="radio<%=i%>"/><%=rs. getString( "opt4")%>< /td></tr>
<%
i++;
}
%>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<%@page import="java.sql.*"%>
<form method="post" action="quizresult.jsp">
<table>
<%
Class.forName("com.mysql.j
Connection connection = DriverManager.getConnectio
Statement st=connection.createStatem
//ResultSet rs=st.executeQuery("Select
ResultSet rs=st.executeQuery("SELECT
int i=1;
while(rs.next()){
%>
<tr><td><%=i%></td>
<td><%=rs.getString("quest
<tr> <td><input type="radio" value="<%=rs.getString("op
<tr> <td><input type="radio" value="<%=rs.getString("op
<tr> <td><input type="radio" value="<%=rs.getString("op
<tr> <td><input type="radio" value="<%=rs.getString("op
<%
i++;
}
%>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers 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.
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("quizan
Then get the session attribute in the answer page so you know the answers in the right order.