java简单投票系统_JSP实现的简单Web投票程序代码
本文實(shí)例講述了JSP實(shí)現(xiàn)的簡(jiǎn)單Web投票程序。分享給大家供大家參考。具體如下:
這里使用文本文件作為數(shù)據(jù)存儲(chǔ)的投票系統(tǒng)。
1. vote.java:
package vote;
import java.io.*;
import java.util.*;
public class vote {
public String filePath = "";
public int n;
private File voteFile;
private BufferedReader fileRead;
private PrintWriter fileWrite;
public String systemMessage = "";
private String voteStr[] = new String[10];
public int voteNum[] = new int[10];
public void createFile() throws FileNotFoundException {
voteFile = new File(filePath);
if (!voteFile.exists()) {
fileWrite = new PrintWriter(new FileOutputStream(filePath));
for (int i = 0; i < n; i++)
fileWrite.println("0");
fileWrite.close();
}
}
public void writeFile() throws FileNotFoundException {
fileWrite = new PrintWriter(new FileOutputStream(filePath));
for (int i = 0; i < n; i++) {
fileWrite.println(voteNum[i]);
}
fileWrite.close();
}
public void readFile() throws FileNotFoundException {
fileRead = new BufferedReader(new FileReader(filePath));
for (int i = 0; i < n; i++) {
try {
voteStr[i] = fileRead.readLine();
} catch (IOException f) {
voteStr[i] = "0";
}
voteNum[i] = Integer.parseInt(voteStr[i]);
}
try {
fileRead.close();
} catch (IOException d) {
systemMessage = d.toString();
}
}
}
2. vote.jsp:
String vote1=request.getParameter("lang");
vote.n=4;
vote.filePath="vote.txt";
vote.createFile();
vote.readFile();
if(vote1.compareTo("0")==0)
vote.voteNum[0]++;
if(vote1.compareTo("1")==0)
vote.voteNum[1]++;
if(vote1.compareTo("2")==0)
vote.voteNum[2]++;
if(vote1.compareTo("3")==0)
vote.voteNum[3]++;
vote.writeFile();
%>
alert("感謝你投了寶貴的一票");
self.location="index.jsp";
3. see.jsp:
String vote1=request.getParameter("lang");
vote.n=4;
vote.filePath="vote.txt";
vote.createFile();
vote.readFile();
int total=0;
float voteFlo[]=new float[5];
for(int i=0;i<4;i++) total+=vote.voteNum[i];
for(int i=0;i<4;i++) voteFlo[i]=150*((float)vote.voteNum[i]/(float)total);
%>
查看調(diào)查| 調(diào)查結(jié)果 | |
| JSP | height=8> |
| ASP | height=8> |
| PHP | height=8> |
| 其他 | height=8> |
| 關(guān)閉窗口 | |
4. index.jsp:
投票function cw()
{
window.open("see.jsp","mywindow",
"toolbar=no,left=150,top=200,width=270,height=350,menubar=no,systemMenu=no");
}
|
希望本文所述對(duì)大家的JSP程序設(shè)計(jì)有所幫助。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java简单投票系统_JSP实现的简单Web投票程序代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java的collections_Jav
- 下一篇: 酷派手机android版本,系统版本迎来