JSP实现的简单Web投票程序代码

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 scope="request"/> <% 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("感谢你投了宝贵的一票"); 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 scope="request"/> <% 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); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>查看调查</title> <link href="https://www.jb51.net/article/t1.css" type="text/css"> </head> <body> <table> <tr> <td colspan="2"><div>调查结果</div></td> </tr> <tr> <td>JSP</td> <td><img src="https://www.jb51.net/bar.gif" width=<%=voteFlo[0]%> height=8> <%=vote.voteNum[0]%></td> </tr> <tr> <td>ASP</td> <td><img src="https://www.jb51.net/bar.gif" width=<%=voteFlo[1]%> height=8> <%=vote.voteNum[1]%></td> </tr> <tr> <td>PHP</td> <td><img src="https://www.jb51.net/bar.gif" width=<%=voteFlo[2]%> height=8> <%=vote.voteNum[2]%></td> </tr> <tr> <td>其他</td> <td><img src="https://www.jb51.net/bar.gif" width=<%=voteFlo[3]%> height=8> <%=vote.voteNum[3]%></td> </tr> <tr> <td colspan="2"><div><a href="javascript:window.close();">关闭窗口</a></div></td> </tr> </table> </body> </html>

4. index.jsp:

<%@ page contentType="text/html; charset=gb2312" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>投票</title> <link href="https://www.jb51.net/article/t1.css" type="text/css"> </head> <script language="javascript"> function cw() { window.open("see.jsp","mywindow", "toolbar=no,left=150,top=200,width=270,height=350,menubar=no,systemMenu=no"); } </script> <body> <table> <tr> <td><form method="post" action="vote.jsp"> <table bordercolor="#9966CC"> <tr> <td><div>你所使用的开发语言</div></td> </tr> <tr> <td><input type="radio" value="0"> JSP</td> </tr> <tr> <td><input type="radio" value="1"> ASP</td> </tr> <tr> <td><input type="radio" value="2"> PHP</td> </tr> <tr> <td><input type="radio" value="3"> 其他 </td> </tr> <tr> <td><div> <input type="image" src="https://www.jb51.net/poll.gif"> <a href="javascript:cw()"><img src="https://www.jb51.net/see.gif"></a></div></td> </tr> </table> </form></td> </tr> </table> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfysw.html