JSP内置对象综合实例:投票系统

建立了两个treemap对象,分别建立了选举号与候选人姓名、选举号与所得票数的关系。

<%
TreeMap m=new TreeMap();
TreeMap m2=new TreeMap();
m.put("1001","张1");m2.put("1001", new Integer(0));
m.put("1002","张2");m2.put("1002", new Integer(0));
m.put("1003","张3");m2.put("1003", new Integer(0));
m.put("1004","张4");m2.put("1004", new Integer(0));
m.put("1005","张5");m2.put("1005", new Integer(0));
application.setAttribute("rollpara", m);
application.setAttribute("rollnum", m2);
%>

vote2页面主要有开始选举,浏览票数功能。显示候选人名字的form表单为动态生成的,通过application兑现,获得候选人映射的treemap对象的m,进而获得候选人编号及姓名,产生input多选控件。
同时,通过获取key值,产生超链接,点击候选人的名字可以进入个人介绍页面的静态html。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<%
TreeMap m=(TreeMap)application.getAttribute("rollpara");
Set se=m.keySet();
Iterator it=se.iterator();
String s="<form action=vote3.jsp>";
while(it.hasNext()){
    String key=(String)it.next();
    String value=(String)m.get(key);
    s+="<a href="+key+".html>"+value+"</a>";
    s+="&nbsp&nbsp&nbsp&nbsp&nbsp";
    s+="<input type=checkbox name='roll' value='"+key+"'/></br>";   
}
s+="<input type='submit' value='开始选举'>";
s+="</form>";


%>

<body>
<%=s %>
<form action="vote4.jsp">
<input type="submit" value="浏览票数"/>
</form>
</body>
</html>

vote3页为检测投票者是否合法。通过session变量的mark标志判断。若mark为假,则表示合法,设置mark为true,同时通过遍历application候选人共享的变量m2,是相应候选人的票数+1;若mark为true,表明他已经投过票了,提示无效。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
TreeMap m2=(TreeMap)application.getAttribute("rollnum");
Boolean mark=(Boolean)session.getAttribute("mark");
if(mark==null){
    session.setAttribute("mark",new Boolean(true));
    String roll[]=request.getParameterValues("roll");
    if(roll!=null){
        for(int i=0;i<roll.length;i++){
            Integer cur=(Integer)m2.get(roll[i]);
            cur++;
            m2.put(roll[i],cur);

        }
    }
    out.print("选举成功!</br>");
}else{
    out.print("你已经选举完毕,不能再选<br>");
}
%>
<body>
<a href="vote2.jsp">返回</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
TreeMap m=(TreeMap)application.getAttribute("rollpara");
TreeMap m2=(TreeMap)application.getAttribute("rollnum");
Set se=m.keySet();
Iterator it=se.iterator();

String s="<table border=1>";
s+="<tr><th>编号</th><th>姓名</th><th>票数</th></tr>";
while(it.hasNext()){
    String no=(String)it.next();
    String name=(String)m.get(no);
    Integer num=(Integer)m2.get(no);

    s+="<tr>";
    s+="<td>"+no+"</td>";
    s+="<td>"+name+"</td>";
    s+="<td>"+num+"</td>";
    s+="</tr>";
}
%>
<body>
<%=s %>
<a href="vote2.jsp">返回</a>
</body>
</html>

这里写图片描述

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值