简单实用的JavaWeb微投票系统

简单实用的JavaWeb微投票系统
目的

帮她完成JavaWeb课程设计,也做为练手的小项目;
功能

1、可以实现在个人登陆,在线投票,票数统计的功能;
2、规定每个投票者每天只能投一票,并要求登录成功后才可以投票;
分块

1、Login类:实现登录
2、Vote类:实现投票
3、Vote_Filter类:过滤器类,控制登录和刷票
4、UserService类:Service类用来调用UserDao操作数据库
5、UserDao类:操作数据库类
6、Voter类:投票者模型类
7、User类: 被投者模型类
8、DBUtils类:Jdbc连接MySQL工具类
数据库(表)

               投票者信息表

字段 数据类型 说明

id int 序号,主键
username varchar 用户名
password varchar 密码
record varchar 最后一天投票时间

            被投者信息表

字段 数据类型 说明
id int 序号,主键
name varchar 用户名
count int 票数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Login类

package cn.sy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.sy.domain.Voter;
import cn.sy.service.UserService;

@WebServlet(name = “LoginServlet”, urlPatterns = { “/login” })
public class Login extends HttpServlet {

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // 1、得到登录名和密码
    String username = request.getParameter("username");// 得到用户输入的登录名
    String password = request.getParameter("password");// 得到用户输入的密码

    // 2、将信息封装
    Voter voter = new Voter();

    voter.setUsername(username);
    voter.setPassword(password);


    UserService service = new UserService();

    try {
        Voter existVoter = service.login(voter);// 3、调用service中登录方法
        if (existVoter == null) {// 代表用户名或密码错误,存储错误信息在request域中,请求转发到login.jsp
            request.setAttribute("message", "登录失败:用户名或密码错误!");
            // 登录失败,请求转发到登录页面显示错误信息message
            request.getRequestDispatcher("/login.jsp").forward(request,
                    response);
            return;
        } else {// 登录成功
                // 存到session里
            request.getSession().setAttribute("user",
                    existVoter.getUsername(http://www.aivote.com/));
            // 重定向到成功页面
            response.sendRedirect(request.getContextPath() + "/success.jsp");
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

login.jsp登录页面

<%@ page language=“java” import=“java.util.*” %>
<%@ page contentType=“text/html;charset=UTF-8” pageEncoding=“UTF-8” %>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<meta name="description" content="">
<meta name="author" content="">
<title>登录</title>
<link href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.css" rel="stylesheet">
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="login.css">

sy的投票系统

这里写图片描述
success.jsp登录成功页面

<%@ page language=“java” import=“java.util.*” pageEncoding=“utf-8”%>

登录成功!
  <div class="site-wrapper-inner">

    <div class="cover-container">

      <div class="masthead clearfix">
        <div class="inner">
          <h3 class="masthead-brand">${user}</h3>
          <nav>
            <ul class="nav masthead-nav">
              <li class="active"><a href="#">首页</a></li>
              <li><a href="#">关于</a></li>
              <li><a href="/Voting_System/login.jsp">退出</a></li>
            </ul>
          </nav>
        </div>
      </div>

      <div class="inner cover">
        <!--用EL语句调用出存到session里的usename  -->
        <h1 class="cover-heading">欢迎${user}来投票</h1>
        <p class="lead"></p>
        <p class="lead">
          <a href="vote.jsp" class="btn btn-lg btn-default">点击投票</a>
        </p>
      </div>

      <div class="mastfoot">
        <div class="inner">
          <p>sy的投票系统 </p>
        </div>
      </div>

    </div>

  </div>

</div>
以下是一个基于Javaweb的简易投票系统的实现方法: 1.创建数据库表 首先,我们需要创建一个数据库表来存储投票信息。可以创建一个名为vote的表,包含以下字段: - id:投票ID,自增长整数类型 - title:投票标题,字符串类型 - option_a:选项A,字符串类型 - option_b:选项B,字符串类型 - option_c:选项C,字符串类型 - option_d:选项D,字符串类型 - create_time:创建时间,日期时间类型 2.创建Javaweb项目 接下来,我们需要创建一个Javaweb项目。可以使用Eclipse或者IntelliJ IDEA等开发工具创建一个名为vote的动态Web项目。 3.编写投票页面 在Web项目中,我们需要编写一个投票页面,包含投票标题和四个选项。可以使用HTML和CSS编写一个简单的页面,如下所示: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>投票系统</title> <style> body { font-family: Arial, sans-serif; background-color: #f0f0f0; } .container { margin: 0 auto; width: 600px; padding: 20px; background-color: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); } h1 { text-align: center; margin-bottom: 20px; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 10px; font-weight: bold; } input[type="text"] { width: 100%; padding: 10px; border-radius: 5px; border: 1px solid #ccc; box-sizing: border-box; } input[type="submit"] { display: block; margin: 0 auto; padding: 10px 20px; background-color: #4CAF50; color: #fff; border: none; border-radius: 5px; cursor: pointer; } </style> </head> <body> <div class="container"> <h1>投票系统</h1> <form action="vote" method="post"> <div class="form-group"> <label for="title">投票标题:</label> <input type="text" id="title" name="title" required> </div> <div class="form-group"> <label for="option_a">选项A:</label> <input type="text" id="option_a" name="option_a" required> </div> <div class="form-group"> <label for="option_b">选项B:</label> <input type="text" id="option_b" name="option_b" required> </div> <div class="form-group"> <label for="option_c">选项C:</label> <input type="text" id="option_c" name="option_c" required> </div> <div class="form-group"> <label for="option_d">选项D:</label> <input type="text" id="option_d" name="option_d" required> </div> <input type="submit" value="提交"> </form> </div> </body> </html> ``` 4.编写Servlet 在Javaweb项目中,我们需要编写一个Servlet来处理投票请求。可以创建一个名为VoteServlet的Servlet,实现doGet和doPost方法。在doPost方法中,我们需要获取投票信息并将其保存到数据库中。 ```java import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class VoteServlet extends HttpServlet { private static final long serialVersionUID = 1L; public VoteServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String title = request.getParameter("title"); String optionA = request.getParameter("option_a"); String optionB = request.getParameter("option_b"); String optionC = request.getParameter("option_c"); String optionD = request.getParameter("option_d"); Date createTime = new Date(); Connection conn = null; PreparedStatement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vote?useSSL=false", "root", "123456"); stmt = conn.prepareStatement("INSERT INTO vote (title, option_a, option_b, option_c, option_d, create_time) VALUES (?, ?, ?, ?, ?, ?)"); stmt.setString(1, title); stmt.setString(2, optionA); stmt.setString(3, optionB); stmt.setString(4, optionC); stmt.setString(5, optionD); stmt.setTimestamp(6, new java.sql.Timestamp(createTime.getTime())); stmt.executeUpdate(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } response.sendRedirect("success.html"); } } ``` 5.编写成功页面 最后,我们需要编写一个成功页面,用于提示用户投票信息已经成功保存到数据库中。可以使用HTML和CSS编写一个简单的页面,如下所示: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>投票系统</title> <style> body { font-family: Arial, sans-serif; background-color: #f0f0f0; } .container { margin: 0 auto; width: 600px; padding: 20px; background-color: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); } h1 { text-align: center; margin-bottom: 20px; } p { text-align: center; margin-bottom: 20px; font-size: 24px; font-weight: bold; color: #4CAF50; } </style> </head> <body> <div class="container"> <h1>投票系统</h1> <p>投票信息已经成功保存到数据库中!</p> </div> </body> </html> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值