Simple Web voting code and jspweb Code implemented by JSP

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.