此程序是在前人的版本中,经本博主认真调试更改完善的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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>投票</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script language = "javascript">
function cw(){ //弹出查看投票结果窗口
window.open("see.jsp","mywindow","left = 550,top = 200,width = 270,height = 350");//menubar:是否显示菜单栏 ;toolbar:是否显示工具栏 ;systemMenu:系统菜单
}
function check(){ //判断投票是否选择
var result=false;
for(var i=0;i<document.form1.lang.length;i++){
if(document.form1.lang[i].checked){
result = true;
}
}
if(!result){
alert("请选择投票!");
}
return result;
}
</script>
<body>
<table width = "15%" height = "250" align = "center">
<tr>
<td><form name = "form1" method = "post" action = "vote.jsp" onsubmit = "return check()">
<!-- 投票界面内容 -->
<table width = "100%" height = "250" border = "1" align = "center" bordercolor = "#9966CC" class = "t1" style="height: 325px; width: 230px; ">
<tr>
<td><div align = "left">你所使用的开发语言</div></td>
</tr>
<tr>
<td style = "width = 116px;height = 22px;"><input type = "radio" name = "lang" value = "0">JSP</td>
</tr>
<tr> <!-- checked 默认选择 -->
<td><input type = "radio" name = "lang" value = "1">ASP</td>
</tr>
<tr>
<td><input type = "radio" name = "lang" value = "2">PHP</td>
</tr>
<tr>
<td><input type = "radio" name = "lang" value = "3">其他</td>
</tr>
<tr>
<td>
<!-- 采用相对位置 浮动定位 -->
<div style = "position:relative;right:50px;top:25%;float:right"><input name = "vote" type = "image" src = "Image/poll.gif" width = "40" height = "40" border = "0" alt = "投票"></div><!-- onclick = "return false" -->
<div style = "position:relative;left:50px;top:25%;float:left"><a href = "javascript:cw()"><img src = "Image/see.gif" width = "40" height = "40" border = "0" alt = "查看结果"></a></div> <!-- title -->
</td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
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);
%>
<html>
<head>
<base href="<%=basePath%>">
<title>查看调查</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table width = "30%" border = "0" class = "tb1">
<tr>
<td colspan = "2"><div align = "center">调查结果</div></td>
</tr>
<tr>
<td width = "18%">JSP</td>
<td width = "18%"><img src = "Image/bar.png" width = <%= voteFlo[0] %> height = 8><%= vote.voteNum[0] %></td>
</tr>
<tr>
<td width = "18%">ASP</td>
<td width = "18%"><img src = "Image/bar.png" width = <%= voteFlo[1] %> height = 8><%= vote.voteNum[1] %></td>
</tr>
<tr>
<td width = "18%">PHP</td>
<td width = "18%"><img src = "Image/bar.png" width = <%= voteFlo[2] %> height = 8><%= vote.voteNum[2] %></td>
</tr>
<tr>
<td width = "18%">其他</td>
<td width = "18%"><img src = "Image/bar.png" width = <%= voteFlo[3] %> height = 8><%= vote.voteNum[3] %></td>
</tr>
<tr>
<td colspan = "2"><div align = "center"><a href = "javascript:window.close();">关闭窗口</a></div></td>
</tr>
</table>
</body>
</html>
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("<script language = 'javascript'>alert('qing投票。');self.location = 'index.jsp';</script>");
vote.writeFile();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'vote.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script language = "javascript">
alert("感谢你的投票。");
self.location = "index.jsp";
</script>
</head>
<body>
</body>
</html>