JSP實(shí)現(xiàn)的簡(jiǎn)單Web投票程序代碼
本文實(shí)例講述了JSP實(shí)現(xiàn)的簡(jiǎn)單Web投票程序。分享給大家供大家參考。具體如下:
這里使用文本文件作為數(shù)據(jù)存儲(chǔ)的投票系統(tǒng)。
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("感謝你投了寶貴的一票"); 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); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>查看調(diào)查</title> <link href="t1.css" rel="stylesheet" type="text/css"> </head> <body> <table width="30%" border="0" class="t1"> <tr> <td colspan="2"><div align="center">調(diào)查結(jié)果</div></td> </tr> <tr> <td width="18%">JSP</td> <td width="82%"><img src="bar.gif" width=<%=voteFlo[0]%> height=8> <%=vote.voteNum[0]%></td> </tr> <tr> <td>ASP</td> <td><img src="bar.gif" width=<%=voteFlo[1]%> height=8> <%=vote.voteNum[1]%></td> </tr> <tr> <td>PHP</td> <td><img src="bar.gif" width=<%=voteFlo[2]%> height=8> <%=vote.voteNum[2]%></td> </tr> <tr> <td>其他</td> <td><img src="bar.gif" width=<%=voteFlo[3]%> height=8> <%=vote.voteNum[3]%></td> </tr> <tr> <td colspan="2"><div align="center"><a href="javascript:window.close();">關(guān)閉窗口</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="t1.css" rel="stylesheet" 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 width="15%" height="250" align="left"> <tr> <td><form name="form1" method="post" action="vote.jsp"> <table width="100%" height="250" border="1" align="center" bordercolor="#9966CC" class="t1"> <tr> <td><div align="left">你所使用的開(kāi)發(fā)語(yǔ)言</div></td> </tr> <tr> <td><input type="radio" name="lang" value="0"> JSP</td> </tr> <tr> <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 align="center"> <input name="vote" type="image" src="poll.gif" width="40" height="20" border="0"> <a href="javascript:cw()"><img src="see.gif" width="40" height="20" border="0"></a></div></td> </tr> </table> </form></td> </tr> </table> </body> </html>
希望本文所述對(duì)大家的JSP程序設(shè)計(jì)有所幫助。
相關(guān)文章
JSP MySQL插入數(shù)據(jù)時(shí)出現(xiàn)中文亂碼問(wèn)題的解決方法
這篇文章主要介紹了JSP MySQL插入數(shù)據(jù)時(shí)出現(xiàn)中文亂碼問(wèn)題的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11struts2 action跳轉(zhuǎn)調(diào)用另一個(gè)程序
主要為了在一個(gè)Action成功后跳轉(zhuǎn)調(diào)用另一個(gè)程序,需要的朋友可以參考下2012-11-11JSP/JAVABEAN+TOMCAT4.0.5+MYSQL組合建站總結(jié)
JSP/JAVABEAN+TOMCAT4.0.5+MYSQL組合建站總結(jié)...2006-10-10win2000server IIS和tomcat5多站點(diǎn)配置
win2000server IIS和tomcat5多站點(diǎn)配置...2006-10-10JSP response對(duì)象實(shí)現(xiàn)文件下載的兩種方式
這篇文章主要介紹了JSP隱含對(duì)象response實(shí)現(xiàn)文件下載的兩種方法,很簡(jiǎn)單,但很實(shí)用,需要的朋友可以參考下2014-09-09ssh生成隨機(jī)數(shù)字驗(yàn)證碼操作步驟
隨機(jī)數(shù)字驗(yàn)證碼生成方法很多,接下來(lái)將介紹下jsp中時(shí)如何實(shí)現(xiàn)的,感興趣的朋友可以參考下,希望本文對(duì)你有所幫助2013-02-02JSP中使用JDBC訪問(wèn)SQL Server 2008數(shù)據(jù)庫(kù)示例
這篇文章主要介紹了JSP中使用JDBC訪問(wèn)SQL Server 2008數(shù)據(jù)庫(kù)示例,本文重點(diǎn)在JSP代碼示例中,需要的朋友可以參考下2014-09-09JSP學(xué)習(xí)之Java Web中的安全控制實(shí)例詳解
這篇文章主要介紹了JSP學(xué)習(xí)之Java Web中的安全控制,較為詳細(xì)的分析了JSP安全控制的常見(jiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09JSP頁(yè)面中如何用select標(biāo)簽實(shí)現(xiàn)級(jí)聯(lián)
在JSP頁(yè)面中如何用select標(biāo)簽實(shí)現(xiàn)級(jí)聯(lián)呢?以下小編就為大家介紹實(shí)現(xiàn)方法,需要的朋友可以參考下2013-07-07JSP對(duì)URL鏈接中的中文亂碼處理方法總結(jié)
這篇文章主要介紹了JSP對(duì)URL鏈接中的中文亂碼處理方法總結(jié),注意一下各瀏覽器的兼容顯示情況,需要的朋友可以參考下2016-01-01