jsp中的多项选择题 [英] Multiple choice questions in jsp
问题描述
我正在开发一个网页,我将基于简单的JSP-MsAccess
连接进行小测验。但是当我运行我的测验时,当我回答问题时,结果将应用于所有问题意味着我得到第一个问题然后我的分数是10,如果我得到第一个答案错误我的分数是0.
代码:
I am developing a webpage where i am going to conduct a small quiz based on plain JSP-MsAccess connection.But when I run my quiz and when I answer a question the result is applied to all questions means if I get first question right then my score is 10 and if I get first answer wrong my score is 0. code:
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String ans=" ";
if(request.getParameter("correctAns")!=null)
{
ans=request.getParameter("correctAns").toString();
}
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String id=request.getParameter("id");
System.out.println("id:"+id);
int i=1;
String s,g;
int count=0;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:qdsn");
rs = null;
st = conn.createStatement();
//for(i=1;i<=2;i++)
// {
rs = st.executeQuery("select * from exam");
while(rs.next()) {
%>
<br/>
<center>
<table border="1" width="500px" bgcolor="pink" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<form name="form1">
<h2 align="center"><font color="red">Online Quiz Application</font></h2>
<b>Select Correct Answer</b>
<table border="0" width="500px" cellspacing="2" cellpadding="4">
<tr>
<td width="50%"> Question:</td>
<input type="hidden" name="correctAns" value="<%=rs.getString(6)%>" />
<tr>
<td><%= rs.getString("quest") %></td></tr>
<tr>
<td>
1: <input type="radio" name="a" value= "QA" /></td>
<td><%= rs.getString("QA") %></td></tr>
<tr>
<td>
2: <input type="radio" name="a" value="QB" /></td>
<td><%= rs.getString("QB") %></td></tr>
<tr>
<td>
3: <input type="radio" name="a" value="QC" /></td>
<td><%= rs.getString("QC") %> </td></tr>
<tr>
<td>
4: <input type="radio" name="a" value="QD" /> </td>
<td> <%= rs.getString("QD") %> </td></tr>
<tr>
<td>
<center>
<input type="submit" value="Submit" name="submit"></center></td></tr>
</table>
</form>
</td>
</tr>
</table>
</center>
</body>
<% g=request.getParameter("a");
%>
<%
if(g.equals(ans)){
count++;
}
%>
<%
}}
// }
catch (Exception ex) {
ex.printStackTrace();
%>
<%
} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
}
out.println("Score="+count);
%>
</html>
推荐答案
更新您的单选按钮名称以使用某些索引,即
Update your radio button name to use some index i.e.
在顶部:声明索引变量。
At the top side: declare the index variable.
<% int qNum = 0; %>
在页面中间,使用 radio $ c中的索引变量$ c>字段命名。
In middle of page, use the index variable in radio
field naming.
<input type="radio" name="a<%=qNum%>" value="QA" />
<input type="radio" name="a<%=qNum%>" value="QB" />
<input type="radio" name="a<%=qNum%>" value="QC" />
<input type="radio" name="a<%=qNum%>" value="QD" />
在页面底部,检索索引参数并将索引增加一。
In the bottom of the page, retrieve the index parameter and increase the index by one.
<% g=request.getParameter("a"+qNum);
qNum++;
%>
这篇关于jsp中的多项选择题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!