jsp综合应用之标准化考试

来源:互联网 发布:单片机模块化编程 编辑:程序博客网 时间:2024/06/05 16:39


实验要求:

使用bean来管理试题文件,jsp页面调用bean实现网络标准化考试。

当考试者选择一套试题,并确定开始考试后,bean负责读入试题,并显示给考试者。当考试者在jsp页面提供的文本框输入全部的答案,单击提交键后,jsp页面再次调用bean来判断考试者的得分,而且考试者只有一次提交答案的机会,bean还负责将考试者的成绩保存到文件。

实验步骤:

1.创建bean的源文件test.java

package com.wxh;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.RandomAccessFile;public class Test {String fileName="",correctAnswer="",testContent="",selection="";int score=0;int giveAnswerCount=0;String name="",number="";public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;selection="";score=0;giveAnswerCount=0;}public String getCorrectAnswer() {try{File f=new File("F:\2000",fileName);FileReader in=new FileReader(f);BufferedReader buffer=new BufferedReader(in);correctAnswer=(buffer.readLine()).trim();buffer.close();in.close();}catch(Exception e){}if(giveAnswerCount==1){return correctAnswer;}else{return "你只有一次提交答案的机会,不允许2次提交答案";}}public void setCorrectAnswer(String correctAnswer) {this.correctAnswer = correctAnswer;}public String getTestContent() {StringBuffer temp=new StringBuffer();try{if(fileName.length()>0){File f=new File("F:\2000",fileName);FileReader in=new FileReader(f);BufferedReader buffer=new BufferedReader(in);String str=buffer.readLine();while((str=buffer.readLine())!=null){temp.append("\n"+str);}buffer.close();in.close();}}catch(Exception e){}return new String(temp);}public void setTestContent(String testContent) {this.testContent = testContent;}public String getSelection() {return selection;}public void setSelection(String selection) {this.selection = selection;}public int getScore() {score=0;if(giveAnswerCount==1){int length1=selection.length();int length2=selection.length();int min=(int)(Math.min(length1,length2));int i=0;while(i<min){if(selection.charAt(i)==correctAnswer.charAt(i))score++;i++;}WriteRecorder teacher=new WriteRecorder();teacher.write(name,number,score);}return score;}public void setScore(int score) {this.score = score;}public int getGiveAnswerCount() {return giveAnswerCount;}public void setGiveAnswerCount(int giveAnswerCount) {this.giveAnswerCount = giveAnswerCount;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}}class WriteRecorder{RandomAccessFile random;File f=new File("成绩单.txt");WriteRecorder(){try{random=new RandomAccessFile(f,"rw");}catch(Exception ee){}}public void write(String name,String number,int score){try{random.seek(random.length());String mess="Name:"+name+","+number+","+score;random.writeBytes(mess);random.close();}catch(Exception ee){System.out.println(ee);}}}

2.设计jsp页面


<span style="font-family: 宋体; white-space: pre;"><%@ page import="com.wxh.* "%></span><span style="font-family: 宋体; white-space: pre;"></span>
<body><jsp:useBean id="test" class="com.wxh.Test" scope="session" /><jsp:setProperty name="test" property="name" param="name" /><jsp:setProperty name="test" property="number" param="number" /><jsp:setProperty name="test" property="fileName" param="fileName"/><form action="" method="post" name=form1>输入姓名:<input type="text" name="name" size=10>输入学号:<input type="text" name="fileName" size=10>请选择试题:<select name="fileName" value="A.txt"><option value=A.txt>A.txt</option><option value=B.txt>B.txt</option><option value=C.txt>C.txt</option></select><input type="submit" name="sub" value="确定"></form><TextArea Rows="8" Cols="66"><jsp:getProperty name="test" property="testContent" /></TextArea><jsp:setProperty name="test" property="giveAnswerCount" param="giveAnswerCount"/><form action="" method="post" name=form2>在文本框输入全部题目的答案,答案之间不允许有空格:<br/><input type="text" name="selection" size=70><input type="hidden" name="giveAnswerCount"value="<%=test.getGiveAnswerCount()+1%>"><br><input type="submit" value="提交"></form><jsp:setProperty name="test" property="selection"/>试题的正确答案:<jsp:getProperty name="test" property="correctAnswer" /><br>您提交的答案:<jsp:getProperty name="test" property="selection"/><br>您的分数:<jsp:getProperty name="test" property="score" /></body>

注意事项:

1、在jsp页面注意javabean包的导入。

2、对文件的读取,在相关的文件路径下创建相应的文件。(根据上面的代码可以在自己电脑的F盘里创建一个2000文件夹,再在里面分别新建A.TXT、B.TXT、C.TXT)

思考:

1、如何添加相应的考试文档,使这个系统真正运行。

2、代码改进,添加注释。

0 0
原创粉丝点击