本人做了一个在线投票系统。现在想加入一个限制重复投票功能,具体代码不知道怎么实现,大家帮帮忙吧!
showquestion.jsp如下:<%@ page contentType="text/html; charset=gb2312" language="java" errorPage=""%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="sqltest" class="database.opendata" scope="page">
</jsp:useBean>
<html>
<head>
<title>在线答题</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<body>
<form action = thanks.jsp method="post">
<%
String permit;
String SqlString="SELECT Question,QuestionID from questions where IsVisable = 1";
try
{
ResultSet rs=null;
ResultSet ch_rs=null;
//设置连接
// sqltest.setConnection("com.microsoft.jdbc.sqlserver.SQLServerDriver","jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=questionnaire","sa","");
//设置SQL语句
rs=sqltest.executeQuery(SqlString);
//得到查询结果
//rs=sqltest.getResult();
int questionid = 0,itemid = 0;
int i=0,j; //题目及选项标号
while(rs.next())
{
i++;
String question = rs.getString(1);
questionid= rs.getInt(2);
out.print("<TABLE borderColor=#999999 cellSpacing=0 cellPadding=0 width=100% border=0><tr bgcolor=#999999><td>"+" "+i+"."+" "+question+"</td></tr>"); String ch_sql = "SELECT Item,ItemID From Items Where QuestionID ='"+questionid+"'";
// sqltest.setQuerystatement(ch_sql);
//ch_rs=sqltest.getResult();
ch_rs=sqltest.executeQuery(ch_sql);
j = 0;
while(ch_rs.next())
{
j++;
String item = ch_rs.getString(1);
itemid = ch_rs.getInt(2);
%>
<td >
<%=j%><input type=radio value="<%=itemid%>" name="<%=questionid%>"><%=item%>
</td></tr>
<%
}
out.print(" </table><p>");
}
}
catch(SQLException e1)
{
out.print("SQL异常!");
}%>
<hr>
<DIV align=center><INPUT class=buttonface type=submit value=提交 name=Submit>
<INPUT class=buttonface type=reset value=重选 name=Submit2>
</div>
</form>
</body>
</html>
数据处理页面如下:<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page contentType="text/html;charset=GB2312"%>
<%@include file="convert.jsp"%>
<jsp:useBean id="sqltest" class="database.opendata" scope="page">
</jsp:useBean>
<html>
<head>
<title>
感谢您的参与
</title>
<LINK href="style.css" rel=stylesheet>
</head>
<body>
<%
/*
sql = "SELECT QuestionID from questions where IsVisable = 1 ";
rs = smt.executeQuery(sql);
String question = null,item = null,_sql;
Statement _smt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet _rs;
int questionid = 0,itemid = 0;
while(rs.next())
{
questionid = rs.getInt(1);
itemid = Integer.parseInt(request.getParameter(String.valueOf(questionid)));
_sql = "update Items set ItemCount = ItemCount + 1 Where ItemID ="+itemid;
_smt.executeUpdate(_sql);
}
*/
ResultSet rs=null;
ResultSet ch_rs=null;
String sqlstring = "SELECT QuestionID from questions where IsVisable = 1 ";
try
{
rs=sqltest.executeQuery(sqlstring);
int questionid = 0,itemid = 0;
while(rs.next())
{
questionid = rs.getInt(1);
itemid = Integer.parseInt(request.getParameter(String.valueOf(questionid)));
sqlstring = "update Items set ItemCount = ItemCount + 1 Where ItemID ="+itemid;
sqltest.executeUpdate(sqlstring);
}
}
catch(SQLException e1)
{
out.print("SQL异常!");
}%>
<center>
感谢您的参与!<p>
<div align="center"><a href="showresult.jsp">查看投票结果</a></div>
<p>
<div align="center"><a href="Javascript:window.close();">关闭窗口</a></div>
</center></body>
</html>我想用限制IP的方法实现,在线等大家的帮忙。
showquestion.jsp如下:<%@ page contentType="text/html; charset=gb2312" language="java" errorPage=""%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="sqltest" class="database.opendata" scope="page">
</jsp:useBean>
<html>
<head>
<title>在线答题</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<body>
<form action = thanks.jsp method="post">
<%
String permit;
String SqlString="SELECT Question,QuestionID from questions where IsVisable = 1";
try
{
ResultSet rs=null;
ResultSet ch_rs=null;
//设置连接
// sqltest.setConnection("com.microsoft.jdbc.sqlserver.SQLServerDriver","jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=questionnaire","sa","");
//设置SQL语句
rs=sqltest.executeQuery(SqlString);
//得到查询结果
//rs=sqltest.getResult();
int questionid = 0,itemid = 0;
int i=0,j; //题目及选项标号
while(rs.next())
{
i++;
String question = rs.getString(1);
questionid= rs.getInt(2);
out.print("<TABLE borderColor=#999999 cellSpacing=0 cellPadding=0 width=100% border=0><tr bgcolor=#999999><td>"+" "+i+"."+" "+question+"</td></tr>"); String ch_sql = "SELECT Item,ItemID From Items Where QuestionID ='"+questionid+"'";
// sqltest.setQuerystatement(ch_sql);
//ch_rs=sqltest.getResult();
ch_rs=sqltest.executeQuery(ch_sql);
j = 0;
while(ch_rs.next())
{
j++;
String item = ch_rs.getString(1);
itemid = ch_rs.getInt(2);
%>
<td >
<%=j%><input type=radio value="<%=itemid%>" name="<%=questionid%>"><%=item%>
</td></tr>
<%
}
out.print(" </table><p>");
}
}
catch(SQLException e1)
{
out.print("SQL异常!");
}%>
<hr>
<DIV align=center><INPUT class=buttonface type=submit value=提交 name=Submit>
<INPUT class=buttonface type=reset value=重选 name=Submit2>
</div>
</form>
</body>
</html>
数据处理页面如下:<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page contentType="text/html;charset=GB2312"%>
<%@include file="convert.jsp"%>
<jsp:useBean id="sqltest" class="database.opendata" scope="page">
</jsp:useBean>
<html>
<head>
<title>
感谢您的参与
</title>
<LINK href="style.css" rel=stylesheet>
</head>
<body>
<%
/*
sql = "SELECT QuestionID from questions where IsVisable = 1 ";
rs = smt.executeQuery(sql);
String question = null,item = null,_sql;
Statement _smt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet _rs;
int questionid = 0,itemid = 0;
while(rs.next())
{
questionid = rs.getInt(1);
itemid = Integer.parseInt(request.getParameter(String.valueOf(questionid)));
_sql = "update Items set ItemCount = ItemCount + 1 Where ItemID ="+itemid;
_smt.executeUpdate(_sql);
}
*/
ResultSet rs=null;
ResultSet ch_rs=null;
String sqlstring = "SELECT QuestionID from questions where IsVisable = 1 ";
try
{
rs=sqltest.executeQuery(sqlstring);
int questionid = 0,itemid = 0;
while(rs.next())
{
questionid = rs.getInt(1);
itemid = Integer.parseInt(request.getParameter(String.valueOf(questionid)));
sqlstring = "update Items set ItemCount = ItemCount + 1 Where ItemID ="+itemid;
sqltest.executeUpdate(sqlstring);
}
}
catch(SQLException e1)
{
out.print("SQL异常!");
}%>
<center>
感谢您的参与!<p>
<div align="center"><a href="showresult.jsp">查看投票结果</a></div>
<p>
<div align="center"><a href="Javascript:window.close();">关闭窗口</a></div>
</center></body>
</html>我想用限制IP的方法实现,在线等大家的帮忙。
在你的这个基础上的修改方法。
1。数据库要增加一个表,记录某个IP的答题时间,题目编号等。
2。在数据处理页面,先判断用户IP在这个表中是否存在。如果存在,跳转到其他页面。
3。在数据处理页面,更新Items数据表后,把用户答题时间,IP等保存到IP数据表中。
获取用户IP的方法:String userIP = request.getRemoteAddr();
你这样试试看,不知道是不是你想要的。
2.可以用ip,可能局域网内有多个用户投票。
3.加个验证码,需要用户注册,可以防止部分恶意刷机程序。
如果想做到绝对精确不太可能,以上可以有效防止程序被恶意刷票。