赞
踩
简单的java web 投票系统,工具:myeclipse. 本文的所有显示页面都是用html完成的,数据处理都是用jsp页面完成的。
bar.jpg 图片地址:https://pan.baidu.com/s/1KTlyIBV3_fI5d3z8ocsIGg 提取码:ewtj 连接mysql数据库驱动包链接:https://pan.baidu.com/s/1iGmY6U0Q6rNskJg9L14RqQ 提取码:00sr
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> 欢迎登录<BR> <form action="login.jsp"> 输入账号(文本框):<input name="account" type="text" ><BR> 输入密码(密码框):<input name="password" type="password" ><BR> <input type="reset" value="清空"> <input type="submit" value="登录"> </form> <form action="register.html"> <input type="submit" value="注册"> </form> </body> </html>
本页面是用html完成的。 效果展示:文件名称为login.html
<%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%> <%@ include file="db.inc" %> <html> <body> <% String text=request.getParameter("account"); String password=request.getParameter("password"); Connection conn = getConnection(); session.setAttribute("user",text); Statement stat = conn.createStatement(); String sql = "SELECT text,password FROM T_LOGIN"; ResultSet rs = stat.executeQuery(sql); while(rs.next()){ String text1 = rs.getString("text"); String password1 = rs.getString("password"); if(text1.equals(text)&&password1.equals(password)) { %> <jsp:forward page="display.jsp"></jsp:forward> <% } } %> <script type="text/javascript" > window.alert("账号或者密码错误"); window.document.location.href="login.html"; </script> </body> </html>
文件名称为:login.jsp .作用:与数据库中的账号密码进行对比,如果一样,则登录成功,跳转到投票页面,反之失败,提示“账号或者密码错误”。(提示部分是用javascript的window方法实现)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <form action="register.jsp"> 输入账号(文本框):<input name="account" type="text" ><BR> 输入密码(密码框):<input name="password" type="password" ><BR> <input type="reset" value="清空"> <input type="submit" value="注册"> </form> </body> </html>
效果展示: 文件名称:register.html
<%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%> <%@ include file="db.inc" %> <html> <body> <% String text=request.getParameter("account"); String password=request.getParameter("password"); Connection conn = getConnection(); Statement stat = conn.createStatement(); String sql= "SELECT text FROM T_LOGIN"; ResultSet rs = stat.executeQuery(sql); while(rs.next()){ String text1 = rs.getString("text"); out.println(text1); if(text1.equals(text)) { %> <jsp:forward page="fail.html"></jsp:forward> <% } } String sql1 = "INSERT INTO t_login(text,password,sign) VALUES("+text+","+password+",0)"; int i=stat.executeUpdate(sql1); %> <jsp:forward page="succes.html"></jsp:forward> </body> </html>
文件名称为:register.jsp。作用:与数据库的账号进行对比,如果不相同,则注册成功,跳转到提示失败页面fail.html。反之,失败,跳转到提示成功页面succes.html。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <script type="text/javascript" language="javascript"> alert("账号已存在"); window.document.location.href="register.html"; </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <script type="text/javascript" language="javascript"> alert("注册成功"); window.document.location.href="login.html"; </script> </body> </html>
</tr> <% Connection conn = getConnection(); Statement stat = conn.createStatement(); String sql = "SELECT TEACHERNO,TEACHERNAME,VOTE FROM T_VOTE"; ResultSet rs = stat.executeQuery(sql); while(rs.next()){ String teacherno = rs.getString("TEACHERNO"); String teachername = rs.getString("TEACHERNAME"); int vote = rs.getInt("VOTE"); %> <tr bgcolor="pink"> <td><%=teacherno %></td> <td><%=teachername %></td> <td><img src="img/bar.jpg" width="<%=vote%>" height="10"> <%=vote%></td> <td><a href="vote.jsp?teacherno=<%=teacherno%>">投票</a></td> </tr> <% } stat.close(); conn.close(); %> </table> </body> </html>
效果展示: 文件名称:display.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%> <%@ include file="db.inc" %> <html> <body> <% String teacherno = request.getParameter("teacherno"); Connection conn = getConnection(); String name=(String)session.getAttribute("user"); String si="0"; Statement stat = conn.createStatement(); String sql1 = "SELECT sign FROM t_login where text= "+name+" "; ResultSet rs = stat.executeQuery(sql1); while(rs.next()){ String sign = rs.getString("sign"); if(sign.equals(si)) { String sql = "UPDATE T_VOTE SET VOTE=VOTE+1 WHERE TEACHERNO=? "; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,teacherno); ps.executeUpdate(); ps.close(); String sql2 = "UPDATE t_login SET sign=sign+1 WHERE text= "+name+""; PreparedStatement ps1 = conn.prepareStatement(sql2); ps1.executeUpdate(); ps1.close(); conn.close(); %> <jsp:forward page="display.jsp"></jsp:forward> <% } else { %> <jsp:forward page="voteno.html"></jsp:forward> <% } } %> </body> </html>
文件名称:vote.jsp 防刷票功能是通过表T_LOGIN中的sign列完成,每个账号的sign初始值为0,当投过一票后为1,之后就不能投票了,每个账号只能投一次票。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <script type="text/javascript" language="javascript"> alert("该账号已投过票,不能再投"); window.document.location.href="display.jsp"; </script> </body> </html>
文件名称:voteno.html
<%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%> <%! public Connection getConnection() throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root",""); return conn; } %>
文件名称:db.inc
表t_login: 示例: 表t_vote: 示例:
投票页面的内容暂时只能通过数据库中的表来进行修改,例如修改被投票人的编号,名称等等,票数的初始值一般为0,可自行设定。 整个项目的压缩包 链接:https://pan.baidu.com/s/1yQ8x8kLGDFB2Yywydhv2ug 提取码:6a3e