简单实用的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">
请登录
这里写图片描述
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>