JSP implementation of simple Web voting program code _jsp programming

Source: Internet
Author: User
Tags readfile

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.

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.