基于JavaWeb的简易投票系统

Posted Mr.zhou_Zxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于JavaWeb的简易投票系统相关的知识,希望对你有一定的参考价值。

基于JavaWeb的简易投票系统

项目文件
数据库文件

1.工具

IDEA
JDK1.8
Tomcat8.5
mysql

2.MySQL数据库

  • subjects表

在这里插入图片描述

/*
 Navicat Premium Data Transfer

 Source Server         : zxy
 Source Server Type    : MySQL
 Source Server Version : 50710
 Source Host           : localhost:3306
 Source Schema         : java_test

 Target Server Type    : MySQL
 Target Server Version : 50710
 File Encoding         : 65001

 Date: 19/06/2021 16:16:30
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for subjects
-- ----------------------------
DROP TABLE IF EXISTS `subjects`;
CREATE TABLE `subjects`  (
  `id` int(10) NOT NULL COMMENT '主题ID',
  `title` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主题名',
  `totalVotes` int(50) NOT NULL COMMENT '投票人数',
  `viewTimes` int(50) NOT NULL COMMENT '查看次数',
  `createDate` date NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of subjects
-- ----------------------------
INSERT INTO `subjects` VALUES (1, '本次考试难度如何', 30, 100, '2021-06-18');
INSERT INTO `subjects` VALUES (2, '导致空气污染得主要原因是什么', 40, 200, '2021-06-18');
INSERT INTO `subjects` VALUES (3, '你幸福吗', 35, 150, '2021-06-18');

SET FOREIGN_KEY_CHECKS = 1;

  • options表

在这里插入图片描述

/*
 Navicat Premium Data Transfer

 Source Server         : zxy
 Source Server Type    : MySQL
 Source Server Version : 50710
 Source Host           : localhost:3306
 Source Schema         : java_test

 Target Server Type    : MySQL
 Target Server Version : 50710
 File Encoding         : 65001

 Date: 19/06/2021 16:16:38
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for options
-- ----------------------------
DROP TABLE IF EXISTS `options`;
CREATE TABLE `options`  (
  `id` int(10) NOT NULL COMMENT '选项ID',
  `optContent` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '选项内容',
  `vote` int(50) NOT NULL COMMENT '得票数',
  `sid` int(50) NOT NULL COMMENT '主题ID,主题表外键',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `sid`(`sid`) USING BTREE,
  CONSTRAINT `sid` FOREIGN KEY (`sid`) REFERENCES `subjects` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of options
-- ----------------------------
INSERT INTO `options` VALUES (1, '很难', 5, 1);
INSERT INTO `options` VALUES (2, '比较难', 10, 1);
INSERT INTO `options` VALUES (3, '一般', 5, 1);
INSERT INTO `options` VALUES (4, '容易', 3, 1);
INSERT INTO `options` VALUES (5, '很容易', 7, 1);

SET FOREIGN_KEY_CHECKS = 1;

3.准备工作

  • tomcat
    在这里插入图片描述在这里插入图片描述

  • mybatis-3-cfg.xml
    在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/java_test?useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 让主配置文件引用映射配置文件 -->
    <mappers>
        <mapper resource="com/zxy/mapper/UserMapper.xml"/>
    </mappers>
</configuration>
  • lib

Project Structure --》Modoules --》 Dependencies --》点击“+”添加JARS,选中lib目录就可以
在这里插入图片描述

JSP页面

  • index.jsp
    在这里插入图片描述
<%@ page import="com.zxy.pojo.Subjects" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: ZXY
  To change this template use File | Settings | File Templates.
--%>


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主题页面</title>
</head>
<body>
<a href="SubjectServlet">投票系统</a>
</body>
</html>

  • show.jsp
    在这里插入图片描述
<%@ page import="com.zxy.pojo.Subjects" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: ZXY
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主题页面</title>
</head>
<body>
<a href="/SubjectServlet">全部主题 >></a>
<%--  <div style="text-align: center">全部主题 >></div>--%>

<form action="/OptionServlet" method="post">
    <table border="1">
        <tr>
            <th>序号</th>
            <th>主题</th>
            <th>投票/查看</th>
            <th>创建时间</th>
        </tr>
        <%
            List<Subjects> subjects = (List<Subjects>) session.getAttribute("subjects");
            for(Subjects lists:subjects){
        %>
        <tr>
            <td><%=lists.getId()%></td>
            <td>
                <input type="submit" value="<%=lists.getTitle()%>">
            </td>
            <td><%=lists.getTotalVotes()%>/<%=lists.getViewTimes()%></td>
            <td><%=lists.getCreateDate()%></td>
        </tr>
        <%
            }
        %>
    </table>
</form>
</body>
</html>


  • Option.jsp
    在这里插入图片描述在这里插入图片描述
<%@ page import="com.zxy.pojo.Options" %>
<%@ page import="java.util.List" %>
<%@ page import="com.zxy.pojo.Subjects" %><%--
  Created by IntelliJ IDEA.
  User: ZXY
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>投票选项</title>
</head>
<body>
<h2>本次投票难度如何</h2>
<%
    List<Subjects> subjects = (List<Subjects>) session.getAttribute("subject");
    for(Subjects list:subjects){
        if(list.getId() == 1){
%>
<h3><%=list.getViewTimes()%>次查看,共有<%=list.getTotalVotes()%>人投票</h3>
<%
        }
    }
%>
<form action="/UpdateVoteServlet" method="post" id="updateVotes">

    <table border="1">
        <tr>
            <th>序号</th>
            <th>选项</th>
            <th>票数</th>
            <th>投票率</th>
        </tr>
        <%
            List<Options> Options = (List<Options>) session.getAttribute("option");
            subjects = (List<Subjects>) session.getAttribute("subject");
            for(Subjects sub : subjects){
                if(sub.getId() == 1){
                    int sumVote = sub.getTotalVotes();
                    System.out.println("sumvote");
                    System.out.println(sumVote);
                    for(Options lists:Options){
                    double sum = ((double)lists.getVote()/(double)sumVote) * 100;
                        long round = Math.round(sum * 100);
                        double s = round / 100.0;
                        System.out.println("lists.getvote()");
                        System.out.println(lists.getVote());
                        System.out.println("s");
                        System.out.println(s);
        %>
        <tr>
            <td><%=lists.getId()%></td>
            <td><%=lists.getOptContent()%></td>
            <td><%=lists.getVote()%></td>
            <td><%=s%>%</td>
        </tr>
        <%
                       }
                    }
                }
        %>
    </table>
    投票选项<input type="text" name="id" id="id"><br>
    <span id="msg" style="font-size: 12px;color: red">${messageModel.msg}</span> <br>
    <button type="button" id="loginBtn" >投票</button>
</form>


</body>

<script type="text/javascript" src="js/jquery-3.4.1.js"></script>
<script type="text/javascript">

    $("#loginBtn").click(function () {
        var id = $("#id").val();

        if(isEmpty(id)){
            $("#msg").html("投票选项不能为空!");
            return;
        }

        $("#updateVotes").submit()
    });

    /**
     * 判断用户名或者密码为空的函数
     */
    function isEmpty(str) {
        if(str == null || str.trim() == ""){
            return true;
        }
        return false;
    }
</script>
</html>

</html>

  • Votes.jsp

在这里插入图片描述

<%@ page import="com.zxy.pojo.Options" %>
<%@ page import="java.util.List" %>
<%@ page import="com.zxy.pojo.Subjects" %><%--
  Created by IntelliJ IDEA.
  User: ZXY
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>投票选项</title>
</head>
<body>

<form action="/UpdateVoteServlet" method="post">

    <table border="1">
        <tr>
            <th>序号</th>
            <th>选项</th>
            <th>票数</th>
            <th>投票率</th>
        </tr>
        <%
            List<Options> Options = (List<Options>) session.getAttribute("option");
            List<Subjects> subjects = (List<Subjects>) session.getAttribute("subject");
            for(Subjects sub : subjects){
                if(sub.getId() == 1){
                    int sumVote = sub.getTotalVotes();
                    System.out.println("sumvote");
                    System.out.println(sumVote);
                    for(Options lists:Options){
                    double sum = ((double)lists.getVote()/(double)sumVote) * 100;
                        long round = Math.round(sum * 100);
                        double s = round / 100.0;
                        System.out.println("lists.getvote()");
                        System.out.println(lists.getVote());
                        System.out.println("s");
                        System.out.println(s);
        %>
        <tr>
            <td><%=lists.getId()%></td>
            <td><%=lists.getOptContent()%></td>
            <td><%=lists.getVote()%></td>
            <td><%=s%>%</td>
        </tr>
        <%
                       }
                    }
                }
        %>
    </table>
</form>
<a href="/show.jsp"><input type="button" value="返回主题页面"></a>
</body>
</html>

  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>SubjectServlet</servlet-name>
        <servlet-class>com.zxy.controller.SubjectServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SubjectServlet</servlet-name>
        <url-pattern>/SubjectServlet</url-pattern><!-- 访问servlet的路径 -->
    </servlet-mapping>

    <servlet>
        <

以上是关于基于JavaWeb的简易投票系统的主要内容,如果未能解决你的问题,请参考以下文章

《java精品毕设》基于javaweb宠物领养平台管理系统(源码+毕设论文+sql):主要实现:个人中心,信息修改,填写领养信息,交流论坛,新闻,寄养信息,公告,宠物领养信息,我的寄养信息等(代码片段

帮忙用java做个投票系统,发到我邮箱

大神。一个简易的javaweb项目。商城类的。有没有源代码啊。急需!

Java小项目之:投票系统

java做投票系统

java投票排名实现