Simple Web voting code and jspweb Code implemented by JSP
This article describes the simple Web voting program implemented by JSP. Share it with you for your reference. The details are as follows:
Here, text files are used as the 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 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 JSP program design.