您的位置:首页 > Web前端 > JavaScript

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

2017-07-12 16:28 651 查看
建立了两个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+="     ";
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.*" %>
&
ac76
lt;!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>






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: