JSP实现在线投票系统之完美版

       此程序是在前人的版本中,经本博主认真调试更改完善的Web程序。其中实现了文件的创建,单选(radio)是否选择的判断及跳转,鼠标事件的提示(alt)及对文件的读取、存储等等。

下面是实现过程:

vote.java

package vote;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;


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();
		}
	}
}


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    投票
	
	
	    
	
	
	
  
      
  
      
你所使用的开发语言
JSP
ASP
PHP
其他
查看结果
see.jsp  //实现对投票结果的查看
<%@ page language="java" import="java.util.*,java.util.*,java.io.*" pageEncoding="GB2312"%>
<%@ page import="vote.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<%
    String vote1 = request.getParameter("lang");
    vote vote = new vote();
    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);
 %>

  
    
    
    查看调查
    
	
	
	    
	
	
	

  
  
      
调查结果
JSP height = 8><%= vote.voteNum[0] %>
ASP height = 8><%= vote.voteNum[1] %>
PHP height = 8><%= vote.voteNum[2] %>
其他 height = 8><%= vote.voteNum[3] %>
vote.jsp  //实现在线投票 并写入文件
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GB2312"%>
<%@ page import = "vote.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
    String vote1 = request.getParameter("lang");
    vote vote = new vote();
    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] ++;
    if(vote1.compareTo("0") == 0)
        vote.voteNum[0] ++;
    //else out.println("");
    vote.writeFile();
 %>


  
    
    
    My JSP 'vote.jsp' starting page
    
	
	
	    
	
	
	
	
  
  
  
  
  
JSP实现在线投票系统之完美版_第1张图片 JSP实现在线投票系统之完美版_第2张图片 JSP实现在线投票系统之完美版_第3张图片

你可能感兴趣的:(JSP,JSP,投票系统,radio是否选择)