⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pollresult.jsp

📁 这是一个学习ejb的基本程序
💻 JSP
字号:
<%-- 这个JSP脚本是保存和显示投票结果的 --%>

<%@ page language="java"%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page errorPage="errorpage.jsp" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>

<html>
<head>
<title>利用Cookie实现身份验证</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.tableline {  border-color: #CE00CE #ce00ce #ce00ce; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
.buttonline {  background-color: #FFFFFF; border-color: #8080FF #8080ff #8080ff; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
.tableline2 {  border-color: #FF6633 #ff6633 #ff6633; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.tableline {  border-color: #CE00CE #ce00ce #ce00ce; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
.buttonline {  background-color: #FFFFFF; border-color: #8080FF #8080ff #8080ff; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
.tableline2 {  border-color: #FF6633 #ff6633 #ff6633; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
-->
</style>
</head>

<body bgcolor="#FFFFFF">
<table width="100%" border="0" class=tableline>
  <tr> 
    <td height="149"> 
      <table width="100%" border="0" cellpadding="2" cellspacing="2">
        <tr> 
          <td> 
            <div align="center"><font face="方正姚体" size="+3" color="#7777BB"><font face="方正舒体" size="+5">梦幻</font>网信息调查结果</font></div>
          </td>
        </tr>
        <tr> 
          <td>&nbsp;</td>
        </tr>
        <tr align="center"> 
          <td height="128"> 
            <table width="90%" border="0" cellspacing="0" cellpadding="0" class="tableline2">
              <tr> 
                <td colspan="3">&nbsp;</td>
              </tr>
              <tr> 
                <%
                  String islogin = null;
                  String memberName = null;
                  String getPoll = null;
                  Cookie[] pollcookie = request.getCookies();

                  /* 首先验证访问者是否为合法的会员 */

                  if(pollcookie != null)
                    {
                     for(int j = 0;j < pollcookie.length;j++)
                       {
                        if(pollcookie[j].getName().equals("hadlogin"))
                          {
                           islogin = pollcookie[j].getValue();
                          }
                       }
                    }
                  if((islogin == null) || (!(islogin.equals("1"))))
                    {
                     throw new Exception("你还没有登录,请先登录!");
                    }
                  else
                    {
                     memberName = (String)session.getValue("originalusername");
                     getPoll = request.getParameter("radiobutton_poll"); // 获取投票的对象
                    }
                %>
                <td colspan="3" valign="baseline"><font color="#FF33FF" face="楷体_GB2312" size="4"><b><font color="#009933"><%=memberName%>会员,请看投票结果:</font></b></font></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"> 
                  <div align="left"></div>
                </td>
                <%

                  /* 投票结果的保存与显示 */

                  String[] PollResultArr = new String[12];      // 存放票数
                  float[] PollResultGraphicArr = new float[12]; // 投票结果的图形显示比例长度
                  int pollObj = -1;     // 投票的对象的索引值
                  int i = 0;
                  String prefix = null;
                  String oldRecord = null;
                  int currentRecord = 0;
                  int TotalPoll = 0;
                  File xxx = null;
                  FileReader filein = null;
                  BufferedReader filedata = null;
                  PrintWriter pw = null;
                  StringTokenizer st = null;
                  String writeStr = null;
                  String pollresultfilename = "C:\\jswdk-1.0.1\\webpages\\chapter5\\PollResult.inc";

                  if((getPoll.length() < 1) || (getPoll == null))
                    {
                     throw new Exception("对不起,投票系统出错,请重新来一次!");
                    }
                  else
                    {
                     pollObj = Integer.parseInt(getPoll);
                     try
                       {

                        /* 首先从文件中将上一次投票的结果取出来 */

                        xxx=new File(pollresultfilename);     //在相对路径下打开文件IPlist.txt
                        filein = new FileReader(xxx);
                        filedata = new BufferedReader(filein);
                        oldRecord = filedata.readLine();
                       }
                     catch (IOException e)
                       {
                        throw new Exception("读取数据错误.<br>" + e.toString());
                       }
                     if (oldRecord == null)
                       {
                        throw new Exception("没有任何记录");
                       }
                     else
                       {

                        /* 然后,将数据按照“|”分割符分割开来并且存入数组 */

                        st = new StringTokenizer(oldRecord,"|");
                        for(i = 0;i < 12;i++)
                          {
                           PollResultArr[i] = st.nextToken();
                          }

                        /* 按照投票的对象,将对应的票数 + 1 */

                        currentRecord = Integer.parseInt(PollResultArr[pollObj]);
                        currentRecord = currentRecord + 1;
                        PollResultArr[pollObj] = Integer.toString(currentRecord);

                        /* 最后将当前的投票结果保存到文件中 */

                        try
                          {
                           for(i = 0;i < 12;i++)
                             {
                              if(writeStr != null)
                                {
                                 writeStr = writeStr + "|" +PollResultArr[i];
                                }
                              else
                                {
                                 writeStr = PollResultArr[i];
                                }

                              /* 统计一下总的票数 */

                              TotalPoll = TotalPoll + Integer.parseInt(PollResultArr[i]);
                             }
                           pw = new PrintWriter(new FileOutputStream(pollresultfilename));
                           pw.print(writeStr);
                           pw.close();
                          }
                        catch(IOException e)
                          {
                           throw new Exception("写入文件错误"+e.toString());
                          }

                        /* 将投票的结果以图形的方式显示出来 */
                        
                        for(i = 0;i < 12;i++)
                          {
                           PollResultGraphicArr[i] = 800 * (float)(Integer.parseInt(PollResultArr[i])) / (float)TotalPoll;
                          }
                       }
                    }
                %>
                <td width="65%" valign="baseline">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2" valign="baseline">
                  <div align="left"><font size="2">牡 羊 座 (票数:<%=PollResultArr[0]%> 票)</font></div>
                </td>
                <td width="65%" valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[0]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"> 
                  <div align="left"><font size="2">金 牛 座 (票数:<%=PollResultArr[2]%> 票)</font></div>
                </td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[2]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"> 
                  <div align="left"><font size="2">双 子 座 (票数:<%=PollResultArr[4]%> 票)</font></div>
                </td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[4]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"> 
                  <div align="left"><font size="2">巨 蟹 座 (票数:<%=PollResultArr[6]%> 票)</font></div>
                </td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[6]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"> 
                  <div align="left"><font size="2">狮 子 座 (票数:<%=PollResultArr[8]%> 票)</font></div>
                </td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[8]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"> 
                  <div align="left"><font size="2">处 女 座 (票数:<%=PollResultArr[10]%> 票)</font></div>
                </td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[10]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"><font size="2">天 秤 座 (票数:<%=PollResultArr[1]%> 票)</font></td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[1]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"><font size="2">天 蝎 座 (票数:<%=PollResultArr[3]%> 票)</font></td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[3]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"><font size="2">射 手 座 (票数:<%=PollResultArr[5]%> 票)</font></td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[5]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"><font size="2">魔 羯 座 (票数:<%=PollResultArr[7]%> 票)</font></td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[7]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2" valign="baseline"><font size="2">水 瓶 座 (票数:<%=PollResultArr[9]%> 票)</font></td>
                <td valign="baseline"><img src="poll.jpg" width="<%=PollResultGraphicArr[9]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="2"><font size="2">双 鱼 座 (票数:<%=PollResultArr[11]%> 票)</font></td>
                <td><img src="poll.jpg" width="<%=PollResultGraphicArr[11]%>" height="13"></td>
              </tr>
              <tr> 
                <td colspan="3" height="2">&nbsp; </td>
              </tr>
              <tr> 
                <td colspan="3" height="2"> 
                  <div align="center">总共投票数:<font face="华文行楷" size="5"><b><%=TotalPoll%></b></font> 
                    票</div>
                </td>
              </tr>
              <tr> 
                <td colspan="3" height="2">&nbsp;</td>
              </tr>
            </table>
            <font size="2" color="#009900"></font></td>
        </tr>
        <tr> 
          <td height="2"><font size="2" color="#009900"></font></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -