3.参考程序toupiao.html和result.jsp
在result.jsp里加代码:如果投容易的票数超过40就显示"哇,这么多人会JSP!",否则显示"怎么很少人会JSP啊!"
toupiao.html
<html>
<head><title>在线投票</title></head>
<body bgcolor="#FFFFFF">
<table width="300" border="1" cellspacing="0" cellpadding="0" >
<tr><td>投票题目:你认为JSP容易学么?<td></tr>
<form name="toupiao" method="post" action="result.jsp" >
<tr>
<td><input type="radio" name="toup" value="easy" checked>容易<br>
<input type="radio" name="toup" value="general">一般<br>
<input type="radio" name="toup" value="hard">较难<br>
<input type="submit" name="submit" value="提交" >
</td>
</tr>
</form>
</table>
</body>
</html>
运行结果
result.jsp<%@ page import="java.util.* " %>
<%@ page import="java.lang.* " %>
<%@ page import=" java.io.*" %>
<%!long b1,b2,b3;%>
<% String toup=request.getParameter("toup");
String mydata="";
//String FileName="D:"+System.getProperty("file.separator")+"toupiao.txt";
String FileName="toupiao.txt";
FileWriter resultsFile=new FileWriter(FileName,true);
File myfile=new File(FileName);
if(!(myfile.exists())){
PrintWriter toFile=new PrintWriter(resultsFile);
toFile.println("0:0:0");}
FileInputStream tfile=new FileInputStream(FileName);
String str="";
int c;
while((c=tfile.read())!=-1)
str=str+(char)c;
int first=str.indexOf(":");
int last=str.lastIndexOf(":");
int lenth=str.length();
String First=str.substring(0,first);
String Next=str.substring(first+1,last);
String Last=str.substring(last+1,lenth);
tfile.close();
Long a1=new Long(First);
Long a2=new Long(Next);
Long a3=new Long(Last);
b1=a1.longValue();
b2=a2.longValue();
b3=a3.longValue();
if(toup.compareTo("easy")==0) b1=b1+1;
if(toup.compareTo("general")==0) b2=b2+1;
if(toup.compareTo("hard")==0) b3=b3+1;
str=b1+":"+b2+":"+b3;
RandomAccessFile savefile=new RandomAccessFile(FileName,"rw");
savefile.writeBytes(str);
savefile.close();
%>
投票结果:<br>
容易:<img src=dot.gif height=13 width=<%=3*b1%>><%=b1%><br>
一般:<img src=dot.gif height=13 width=<%=3*b2%>><%=b2%><br>
较难:<img src=dot.gif height=13 width=<%=3*b3%>><%=b3%><br>
合计:<%=b1+b2+b3%><br>
<a href="toupiao.html">返回</a>
运行结果