This article illustrates a simple Web polling program implemented by JSP. Share to everyone for your reference. Specifically as follows:
This uses a text file as a voting system for data storage.
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:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import=" java.util.* "%> <%@
page import=" java.lang.* "%>
<% @ Page import= "java.io.*"%>
<jsp:usebean id= "vote" scope= "request" class= "Vote.vote
"/> <% 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 ();
%>
<script language= "JavaScript" >
alert ("Thank you for your valuable vote");
Self.location= "index.jsp";
</script>
3. See.jsp:
<%@ page contenttype= "text/html; charset=gb2312 "%> <%@ page import= java.util.*"%> <%@ page import= "java.lang.*"%> <%@ page import= " Java.io.* "%> <jsp:usebean id=" vote "scope=" request "class=" Vote.vote "/> <% 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); %>
4. index.jsp:
<%@ page contenttype= "text/html; charset=gb2312 "%>
I hope this article will help you with your JSP programming.