webwork实现投票系统;
1.页面部分vote.html
递交和取消按钮用的两js:
js 代码
- <!---->
- function checkform(src){
- var vote=document.getElementsByName("vote");
- var i=0;
- for( i=0;i
- if(vote[i].checked)
- {
- var src="http://***.com/vote1/vote?vote="+vote[i].value;
- }
- }
- window.open(src,null,"height=160,width=550,status=yes,toolbar=no,menubar=no,location=no");
- }
- <!---->
- function cancelForm()
- {
- var vote=document.getElementsByName("vote");
- vote[4].checked=true;
- }
2:创建servlet:Vote.java
webwork通过request.getParameter("vote")获取页面数据;
因为数据不多所以用xml文件存储;通过XMLManager.ReadXML(path)读到Hashtable中;再利用hashtable.get("a")方法取值.进行处理;通过hashtable.put("a",vale)存入hashtable;
HttpSession session=request.getSession();
session.setAttribute("ht",hashtable);将hashtable传给结果页面;
最后:response.sendRedirect("display.jsp");
3:结果页面:display.jsp
<%@page contentType="text/html; charset=gbk"%>
<%@page import="java.util.Hashtable"%>
<%@page import="java.lang.Math"%>
<%
Hashtable ht = (Hashtable) session.getAttribute("ht");
int a1 = Integer.parseInt((String) ht.get("a"));
int b1 = Integer.parseInt((String) ht.get("b"));
int c1 = Integer.parseInt((String) ht.get("c"));
int d1 = Integer.parseInt((String) ht.get("d"));
int e1 = Integer.parseInt((String) ht.get("e"));
int total = a1 + b1 + c1 + d1+e1;
float a = a1;
float b = b1;
float c = c1;
float d = d1;
float e = e1;
%>