Poll Test

来源:互联网 发布:网络自制综艺节目抄袭 编辑:程序博客网 时间:2024/05/18 15:06

这是我的第一个JSP 投票页面测试例子:

1.
F:/Tomcat 5.0/webapps/ROOT/poll
poll.html

2.
F:/Tomcat 5.0/webapps/ROOT/poll/vote
poll_cope.jsp


1。poll.html

<html>
<head>
<title>请你投票</title>

<meta http-equiv = "Content-Type" content = "text/html; charset = gb2312" >
<style type = "text/css">

body { font-family:宋体; font-size:9pt}
   th { font-size: 9pt }

</style>
</head>

<body>
<table border = "0" cellspacing = "0" width = "332" bgcolor = "#F0F8FF"
  bordercolorlight = "#4DA6FF" bordercolordark = "#ECF5FF">

<tr><td colspan=2 align='center'><B><h4>在线调查</h4></B></td></tr>
<tr><td colspan=2 align='center'><h4>你认为哪只队伍能获得04-05赛季的意甲冠军?</h4></td></tr>

<tr>
<td align='center'>
<form method = post action = "vote/poll_cope.jsp">
<input type="hidden" name="survey" value="ChampionResult">
<input type="radio" name="vote" value="milan" checked>AC 米兰<br>
<input type="radio" name="vote" value="juven">尤文图斯<br>
<input type="radio" name="vote" value="inter">国际米兰<br>
<input type="submit" size="4" value="确定">&nbsp;&nbsp;
<input type="reset" size="4" value="重置">
</form>
</td>
</tr>

<tr>
<td colspan=2 align='center'><font color = "blue"><b><a href = "vote/show.jsp">查看结果</a></b>
</td>
</tr>

</table>
</body>
</html>


2.poll_cope.jsp

<%@ page import = "java.util.*,java.lang.*,java.io.*"%>
<%

String polldata = "";
String resultsDir = "F:Tomcat 5.0/webapps/ROOT/poll/vote";

try{

//在这里,定义File 对象myfile 的唯一作用就是用来判断保存投票记录的文件是否存在
File myfile = new File(resultsDir + System.getProperty("file.separator") + "survey.txt");

if( !(myfile.exists())){

//如果文件存在,则先定义FileWriter 对象resultsFile ,再将其赋值给PrintWriter 对象toFile
FileWriter resultsFile = new FileWriter(resultsDir + System.getProperty("file.separator")
 + "survey.txt", true);

//使用PrintWriter 的方法println() 来写入数据,保存投票结果
PrintWriter toFile = new PrintWriter(resultsFile);

//使用枚举类Enumeration 对象获取客户端的所有请求
Enumeration names = request.getParameterNames();
while(names.hasMoreElements())
{
 String name = (String)names.nextElement();
 String value = request.getParameterValues(name)[0];
 /*
 if(name.compareTo("submit")!=0) {
  out.println(name + ":" + value);
  //RAFile.writeBytes(name + ":" + value + "/r/n");
  toFile.println(name + ":" + value);
 }
 */
 if(value.compareTo("milan")==0) {
  polldata="1:0:0";
  toFile.println(polldata);
 }
 if(value.compareTo("juven")==0) {
  polldata="0:1:0";
  toFile.println(polldata);  
 }
 if(value.compareTo("inter")==0) {
  polldata="0:0:1";
  toFile.println(polldata);
 }
}//while(names.hasMoreElements())

//关闭文件对象
resultsFile.close();
toFile.close();

}//if( !(myfile.exists()))
else{

    //使用RandomAccessFile 类可读型对象("r")的方法readLine() 读取文件数据
 RandomAccessFile RAFile_read = new RandomAccessFile(resultsDir
  + System.getProperty("file.separator") + "survey.txt", "r");

 //使用RandomAccessFile 类写入型对象("rw")的方法writeBytes() 写入投票结果
 RandomAccessFile RAFile_write = new RandomAccessFile(resultsDir
  + System.getProperty("file.separator") + "survey.txt", "rw");

    String record =  RAFile_read.readLine();

 //更新投票结果
 int first =record.indexOf(":");
 int last =record.lastIndexOf(":");
 int len = record.length();
 String First = record.substring(0,first);
 String Next = record.substring(first+1,last);
 String Last = record.substring(last+1,len);
 int a1 = Integer.parseInt(First);
 int a2 = Integer.parseInt(Next);
 int a3 = Integer.parseInt(Last);

 Enumeration names = request.getParameterNames();
 while(names.hasMoreElements())
 {
  String name = (String)names.nextElement();
  String value = request.getParameterValues(name)[0];
        if(value.compareTo("milan")==0)  a1++;
     if(value.compareTo("juven")==0)  a2++;
     if(value.compareTo("inter")==0)  a3++;
     }//while(names.hasMoreElements())
  
  String b1 = String.valueOf(a1);
  String b2 = String.valueOf(a2);
  String b3 = String.valueOf(a3);

  RAFile_write.writeBytes(b1 + ":" + b2 + ":" + b3);
     out.println(b1 + ":" + b2 + ":" + b3);

  //关闭文件对象
  RAFile_write.close();
  RAFile_read.close();

 }//else

 out.println("谢谢你的参与!");


 //关闭输出对象
 out.close(); 

 }catch(FileNotFoundException ex){ 
  ex.printStackTrace();
  out.println( "File Not Found, Please try again.");
  }
  catch(IOException e){
  e.printStackTrace();
     out.println( "A problem occured while recording your voting , Please try again.");
  };
%>

3.survey.txt(运行后自动生成)
F:/Tomcat 5.0/webapps/ROOT/poll/vote

1236:2:3

原创粉丝点击