[返回]
计算机世界2000年第22期

用ASP实现网上考试

华中理工大学管理学院 胡新明

  随着互连网技术的发展,网上教学将成为人们接受再教育和终身教育的主要形式。在网上学校中,人们可以不受时间和空间的限制,随时随地选学任何地方的任何课程。网上学校的发展对网上考试的发展提出了迫切的要求。

  本文用ASP和Access数据库实现了一个网上考试系统。当用户凭用户名和口令登录时,系统首先检查该用户是否已参加过考试。若是,则进行成绩查询;若否,则从题库中提取考题供用户解答。等用户提交答卷后,系统进行评分并将成绩登记入库。

一、数据库设计

  首先建立一数据库exercise.mdb,其中包括两个表:user 和test。

  user表中包括三个字段:

  1. 字段名称:user;字段类型:文本;字段大小:20。存储用户名。
  2. 字段名称:passwd;字段类型:文本;字段大小:20。存储用户密码。
  3. 字段名称:score;字段类型:数字;字段大小:整型。存储用户成绩。

  test表中有五个字段:

  1. 字段名称:question;字段类型:文本;字段大小:255。存储考试题目。
  2. 字段名称:a;字段类型:文本;字段大小:100。存储选项A的答案。
  3. 字段名称:b;字段类型:文本;字段大小:100。存储选项B的答案。
  4. 字段名称:c;字段类型:文本;字段大小:100。存储选项C的答案。
  5. 字段名称:d;字段类型:文本;字段大小:100。存储选项D的答案。
  6. 字段名称:ans;字段类型:文本;字段大小:2。存储正确答案。

二、程序源代码

  该考试系统已在http://personal.hb.cninfo.net/~pftax 站点上应用,有兴趣的读者可前去查看。下面是该系统中较核心的三段源程序,希望对开发类似系统的读者起到抛砖引玉的作用。
‘Login.asp源程序,检验用户是否合法
< %@ Language=VBScript % > 
< %
name=trim(request(“name"))
passwd=trim(request(“passwd"))
‘检查用户是否输入信息
if name<  > “" and passwd<  > “" then
Set conn = Server.CreateObject(“ADODB.Connection")
conn.Open “driver={Microsoft Access Driver
 (*.mdb)};dbq=" & Server.MapPat(“exercise.mdb")
set rs= server.createobject(“adodb.recordset") 
sql= “select * from  user  where user=
‘" &name& “' and passwd=‘" & passwd & “'"
‘检查用户的合法性
Set rs= conn.Execute(sql) 
if not(rs.eof) then
‘检查用户是否已参加过考试,若是则进行成绩查询
if rs(“score")<  > 0 then
response.write rs(“user")&“的考试成绩是"&rs(“score")
else 
session(“pass")=1
session(“user")=name
response.redirect “test.asp"
end if
else 
Response.Write “对不起,用户或口令不正确!!!"
end if 
else 
end if
% >  
< HTML > 
< HEAD > 
< META NAME=“GENERATOR" Content=
“Microsoft Visual Studio 6.0" > 
< TITLE > 用户口令检查< /TITLE > 
< /HEAD > 
< BODY > 
< FORM action=“login.asp" id=
FORM1 method=post name=FORM1 > 
< P title=“" > &nbsp;< /P > 
< P title=“" > &nbsp;< /P > 
< P title=“" align=center > 用户:
< INPUT id=text1 name=name style=
“HEIGHT:22px; WIDTH: 103px" > < /P > 
< P title=“" align=center > 口令:
< INPUT id=password1 name=passwd style=
“HEIGHT: 23px; WIDTH: 101px" 
type=password > < /P > 
< P title=“" align=center > < INPUT id=
submit1 name=submit1 type=submit value=
“进入 " style=“FONT-SIZE: medium; 
FONT-STYLE: normal; FONT-VARIANT:
 normal; FONT-WEIGHT: bold" title=“" > < /P > 
< P title=“" align=center > 
 < /P > 
< /FORM > 
< /BODY > 
< /HTML > 
‘test.asp源程序,从题库中提取试题供解答
< %@ Language=VBScript % > 
< %
if session(“pass")<  > 1 then
response.redirect “login.asp"
else 
end if
Set  conn = Server.CreateObject(“ADODB.Connection")
conn.Open “driver={Microsoft Access Driver (*.mdb)};
dbq=" & Server.MapPath(“exercise.mdb")
‘提取试题
sql=“select * from test"
Set rs = conn.Execute( sql )
% > 
'计时函数
< SCRIPT LANGUAGE=“JavaScript" > 
var isn1=null;
var isn2=false;
today=new Date();
function stopit(){
 if(isn2){
  clearTimeout(isn1);
  }
 isn2 = false;
 }
function startit(){
 stopit();
 isnclock();
 }
function isnclock(){
 var now=new Date();
 var hrs=now.getHours();
 var min=now.getMinutes();
 var sec=now.getSeconds();
 document.clckh.disp.value=“"+((hrs > 12) ? hrs-12 : hrs); 
 document.clckm.disp.value=((min< 10) ? “0" : “")+min;
 document.clcks.disp.value=((sec< 10) ? “0" : “")+sec;
 document.clck.disp.value=(hrs > =12) ? “p.m." : “a.m.";
 isn1=setTimeout(“isnclock()",1000);
 isn2=true;
 }
< /SCRIPT > 
< HTML > 
< HEAD > 
< META NAME=“GENERATOR" Content=
“Microsoft Visual Studio 6.0" > 
< /HEAD > 
< BODY onLoad=“startit()" BGCOLOR=“FFFFFF" > 
< center > 
‘调用计时函数显示时间
< TABLE BORDER=2 > 
< TR > 
< TD > Time< /TD > < TD > Hour< /TD > < TD > Min
< /TD > < TD > Sec< /TD > < TD > < /TD > 
< /TR > 
< TR > 
< TD > < /TD > 
< TD VALIGN=TOP > < FORM NAME=“clckh" onSubmit=“0" > 
< INPUT TYPE=“text" NAME=“disp" SIZE=2 VALUE =“" > 
< /FORM > < /TD > 
< TD VALIGN=TOP > < FORM NAME=“clckm" onSubmit=“0" > 
< INPUT TYPE=“text" NAME=“disp" SIZE=2 VALUE =“" > 
< /FORM > < /TD > 
< TD VALIGN=TOP > < FORM NAME=“clcks" onSubmit=“0" > 
< INPUT TYPE=“text" NAME=“disp" SIZE=2 VALUE =“" > 
< /FORM > < /TD > 
< TD VALIGN=TOP > < FORM NAME=“clck" onSubmit=“0" > 
< INPUT TYPE=“text" NAME=“disp" SIZE=4 VALUE =“" > 
< /FORM > < /TD > < /TR > 
< /center > 
‘显示试题供解答
< FORM action=“result.asp" id=FORM1 method=post name=FORM1 > 
< P > &nbsp;< /P > 
< P > 
  < %
  i=1
  rs.movefirst
  do while not rs.eof% > 
  < P > &nbsp;&nbsp; < %=rs(“question")% > < /P > 
< TABLE align=center border=1 cellPadding=1
 cellSpacing=1 width=“80%" > 
  < TR > 
  < TD style=“WIDTH: 50%" width=“50%" > 
 < INPUT name=ans< %=i% >  type=radio value=
 “A" > < %=rs(“a")% > < /TD > 
< TD > < INPUT name=ans< %=i% >
 type=radio value=“B" > < %=rs(“b")% > < /TD > < /TR > 
  < TR > 
< TD > < INPUT name=ans< %=i% >  
type=radio value=“C" > < %=rs(“c")% > < /TD > 
< TD > < INPUT name=ans< %=i% >  
type=radio value=“D" > < %=rs(“d")% > < /TD > 
  < /TR > 
< /TABLE > 
< %
i=i+1
rs.movenext
loop
% > 
< /P > 
< P > &nbsp;< /P > 
< P align=center > < INPUT id=submit1 name=
submit1 type=submit value=Submit > < INPUT id=
reset1 name=reset1 type=reset value=Reset > < /P > < /FORM > 
< /BODY > 
< /HTML > 
‘result.asp源程序,对用户答卷评分,并将分数记录入库
< %@ Language=VBScript % > 
< HTML > 
< HEAD > 
< META NAME=“GENERATOR" Content=
“Microsoft Visual Studio 6.0" > 
< /HEAD > 
< BODY > 
< P > &nbsp;< /P > 
< %
name=session(“user")
dim score
Set conn = Server.CreateObject(“ADODB.Connection")
conn.Open “driver={Microsoft Access Driver
 (*.mdb)};dbq=" & Server.MapPath(“exercise.mdb")
sql=“select ans from test"
Set rs = conn.Execute( sql )
ycorrect=0
rsCount=0
‘给出正确答案并评分
Response.Write “正确答案:"
Do while not rs.eof
Response.Write rs(“ans")
rsCount=rsCount+1
	if Request.Form(rsCount)=rs(“ans") then
	ycorrect=ycorrect+1
	end if
rs.movenext
loop
Response.Write “< br > 你的答案:"
score=int(ycorrect/rscount*100)
for i=1 to Request.Form.Count-1
Response.Write Request.Form(i)
next
Response.Write “< br > "
Response.Write “你的成绩:"&score
if score< 60 then Response.Write “ 你不及格!"&“< br > "
else if score > =60 Response.Write “ 及格"&“< br > "
else Response.Write “优秀!"
‘将成绩登记入库
strSql=“insert into user (result) values (“&score&") 
where user=‘”&name&“'"
conn.execute(strSql)
% > 
< /BODY > 
< /HTML >