728x90
๐จ๐ป Code
1. index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="pack.user.UserDTO" %>
<%@page import="pack.user.UserDAO" %>
<%@page import="pack.vote.VoteDAO" %>
<%@page import="pack.vote.VoteDTO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
<style>
@font-face {
font-family: 'BMKIRANGHAERANG';
src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_one@1.0/BMKIRANGHAERANG.woff') format('woff');
font-weight: normal;
font-style: normal;
}
*{
font-family: 'BMKIRANGHAERANG';
text-align: center;
}
body{
background: url(img/night.jpg) no-repeat;
background-size: 100%;
position: relative;
}
h1{
font-size: 150px;
font-weight: 400;
margin-top: 13%;
color: orange;
text-shadow: -3px 0 #000, 0 3px #000, 3px 0 #000, 0 -3px #000;
text-shadow: 2px 2px 2px #000;
}
.w-btn {
position: relative;
border: none;
display: inline-block;
padding: 10px 25px;
border-radius: 15px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
text-decoration: none;
font-weight: 300;
transition: 0.25s;
margin: 10px;
font-size: 38px;
}
.w-btn-yellow {
background-color: #F5C836;
}
.w-btn-yellow-outline:hover {
background-color: #fce205;
color: #6e6e6e;
}
.w-btn:hover {
letter-spacing: 2px;
transform: scale(1.2);
cursor: pointer;
}
</style>
</head>
<body>
<h1>์ผ ์ ์ ์</h1>
<%
request.setCharacterEncoding("UTF-8");
if (session.getAttribute("userId") == null) {
%>
<button class="w-btn w-btn-yellow" onclick="location.href='join.jsp'">ํ์๊ฐ์
</button>
<button class="w-btn w-btn-yellow" onclick="location.href='login.jsp'">๋ก๊ทธ์ธ</button>
<%
}
else if (session.getAttribute("userId") != null) {
String Ss_id = (String)session.getAttribute("userId");
VoteDTO user = null;
VoteDTO checkUser = new VoteDTO(Ss_id);
VoteDAO voteDAO = new VoteDAO();
user = voteDAO.findVoter(checkUser);
try{
if(Ss_id.equals("admin")){
UserDTO manager = new UserDTO(Ss_id);
UserDAO userDAO = new UserDAO();
UserDTO adminInfo = userDAO.findUser(manager);
System.out.println(adminInfo.getAge());
if(adminInfo.getAge()==1){
%>
<button class="w-btn w-btn-yellow" onclick="location.href='open.jsp'">ํฌํ ์์</button>
<%
}else{
%>
<button class="w-btn w-btn-yellow" onclick="location.href='close.jsp'">ํฌํ ์ข
๋ฃ</button>
<%
}
%>
<button class="w-btn w-btn-yellow" onclick="location.href='yeunsung_resultpage.jsp'">ํฌํํํฉ</button>
<%
}
else if(user != null){
%>
<button class="w-btn w-btn-yellow" onclick="location.href='yeunsung_resultpage.jsp'">ํฌํํํฉ</button>
<%
}
else{
if(new UserDAO().findUser(new UserDTO("admin")).getAge()==0) {
%>
<button class="w-btn w-btn-yellow" onclick="location.href='vote.jsp'">ํฌํํ๊ธฐ</button>
<%}else { %>
<button class="w-btn w-btn-yellow" onclick="location.href='yeunsung_resultpage.jsp'">ํฌํํํฉ</button>
<%
}
}
%>
<button class="w-btn w-btn-yellow" onclick="location.href='logout.jsp'">๋ก๊ทธ์์</button>
<%
}catch(Exception e){
e.printStackTrace();
response.sendRedirect("login.jsp");
}
}
else{
}
%>
</body>
</html>
2. join.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.io.IOException"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํ์๊ฐ์
</title>
<style>
@font-face {
font-family: 'BMKIRANGHAERANG';
src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_one@1.0/BMKIRANGHAERANG.woff') format('woff');
font-weight: normal;
font-style: normal;
}
*{
font-family: 'BMKIRANGHAERANG';
text-align: center;
}
body{
background: url(img/night.jpg) no-repeat;
background-size: 100%;
position: relative;
}
.input{
width: 60%;
height: 40px;
border: none;
border-radius: 15px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
transition: 0.25s;
margin: 10px;
font-size: 20px;
opacity: 70%;
}
.pass{
font-family:none;
}
.input::placeholder {
font-family: 'BMKIRANGHAERANG';
text-align: center;
}
.inputBtn {
position: relative;
border: none;
display: inline-block;
padding: 10px 25px;
border-radius: 15px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
text-decoration: none;
font-weight: 300;
transition: 0.25s;
margin: 20px;
font-size: 20px;
background-color: #F5C836;
width: 100px;
}
.inputBtn-outline:hover {
background-color: #fce205;
color: #6e6e6e;
}
.inputBtn:hover {
letter-spacing: 2px;
transform: scale(1.2);
cursor: pointer;
}
.radio{
color: white;
font-size: 20px;
}
span{
font-size: 70px;
}
</style>
</head>
<body>
<div
style="width: 30%; height: 400px; margin-top: 40px; margin-left: 34%; text-align: center; padding-top: 5%; border-radius: 30px;">
<p style="font-size: 30px; color: #ffffff; margin: 0px;">
<span> ํ์๊ฐ์
</span>
</p>
<div style="padding-bottom: 50px; margin-top: 30px;">
<form method="post" action="joinOk.jsp" style="text-align: -webkit-center;">
<fieldset id="regbox" style="text-align: center; border: none;">
<input class="input input_id" type="text" name="id" required="true" maxlength="20" title="๊ณต๋ฐฑ ์ ์ธํ 20์ ๋ฏธ๋ง์ผ๋ก ์์ฑํด์ฃผ์ธ์." pattern="^[\S]+$" placeholder="์์ด๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์." /> <br />
<input class="input input_pw pass" type="password" name="pwd" required="true" maxlength="20" title="20์ ๋ฏธ๋ง์ผ๋ก ์์ฑํด์ฃผ์ธ์." pattern="^[\S]+$" placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์." /><br />
<input class="input input_pwc pass" type="password" name="pwdc" required="true" maxlength="20" title="20์ ๋ฏธ๋ง์ผ๋ก ์์ฑํด์ฃผ์ธ์." pattern="^[\S]+$" placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ํ๋ฒ ๋ ์
๋ ฅํด์ฃผ์ธ์." /><br />
<input class="input input_name" type="name" name="name" required="true" maxlength="10" title="๊ณต๋ฐฑ ์ ์ธํ 10์ ๋ฏธ๋ง์ผ๋ก ์์ฑํด์ฃผ์ธ์." pattern="^[\S]+$" placeholder="์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์" /><br />
<input class="input input_age" type="number" name="age" pattern="^[0-9]$" title="์ง์ ๋ ์์์ ์ง์ผ์ฃผ์ธ์." required="true" placeholder="๋์ด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์" /><br />
<label class="radio"><input type="radio" name="gender" value="male">Male</label>
<label class="radio"><input type="radio" name="gender" value="female">Female</label><br/>
<button class="inputBtn" type="submit" value="๊ฐ์
">๊ฐ์
</button>
<button class="inputBtn" type="button" value="์ทจ์" onclick="location.href='index.jsp'">์ทจ์</button>
</fieldset>
</form>
</div>
</div>
</body>
</html>
3. joinOk.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="pack.user.UserDAO" %>
<%@page import="pack.user.UserDTO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํ์๊ฐ์
</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String pwdc = request.getParameter("pwdc");
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
UserDTO user = null;
UserDTO joinUser = new UserDTO(id, pwd, name, age, gender);
UserDAO userDAO = new UserDAO();
try {
user = userDAO.findUser(joinUser);
//์ด๋ฏธ ์กด์ฌํ๋ ์์ด๋์
๋๋ค.
if (user != null) {
response.sendRedirect("loginFail.jsp");
}
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("join.jsp");
}
if (pwd.equals(pwdc)){
try{
userDAO.join(joinUser);
response.sendRedirect("login.jsp");
}catch (Exception e){
response.sendRedirect("join.jsp");
}
}else{
//๋น๋ฐ๋ฒํธ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ
response.sendRedirect("loginFail.jsp");
}
%>
</body>
</html>
4. login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์ผ์์ ์ - ๋ก๊ทธ์ธ</title>
<style>
@font-face {
font-family: 'BMKIRANGHAERANG';
src:
url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_one@1.0/BMKIRANGHAERANG.woff')
format('woff');
font-weight: normal;
font-style: normal;
}
* {
font-family: 'BMKIRANGHAERANG';
text-align: center;
}
body {
background: url(img/night.jpg) no-repeat;
background-size: 100%;
position: relative;
}
.input {
width: 60%;
height: 40px;
display: inline-block;
border: none;
border-radius: 15px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
transition: 0.25s;
font-size: 20px;
opacity: 70%;
}
.input::placeholder {
font-family: 'BMKIRANGHAERANG';
text-align: center;
}
.inputBtn {
position: relative;
border: none;
display: inline-block;
padding: 10px 25px;
border-radius: 15px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
text-decoration: none;
font-weight: 300;
transition: 0.25s;
margin-bottom: 3px;
font-size: 20px;
background-color: #F5C836;
width: 100px;
}
.inputBtn-outline:hover {
background-color: #fce205;
color: #6e6e6e;
}
.inputBtn:hover {
letter-spacing: 2px;
transform: scale(1.2);
cursor: pointer;
}
.title {
font-size: 60px;
}
.tohome {
font-size: 20px;
}
</style>
</head>
<body>
<!-- ๋ก๊ทธ์ธ ์ ์ ์ด๋ฉด ๋ค์ ์ธ๋ฑ์คํ์ด์ง๋ก -->
<%
if (session.getAttribute("userId") != null) {
response.sendRedirect("index.jsp");
}
%>
<div
style="width: 30%; height: 400px; margin-top: 80px; margin-left: 34%; text-align: center; padding-top: 5%; border-radius: 30px;">
<p style="font-size: 30px; color: #ffffff; margin: 0px;">
<span class="title"> ๋ก๊ทธ์ธ </span>
</p>
<div style="padding-bottom: 50px; margin-top: 50px;">
<form method="post" action="loginOk.jsp"
style="text-align: -webkit-center;">
<fieldset id="regbox" style="text-align: center; border: none;">
<input class="input input_id" type="text" name="id" required="true"
maxlength="20" title="๊ณต๋ฐฑ ์ ์ธํ 20์ ๋ฏธ๋ง์ผ๋ก ์์ฑํด์ฃผ์ธ์." pattern="^[\S]+$"
placeholder="์์ด๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์." /> <br /> <input
class="input input_pw" type="password" name="password"
required="true" maxlength="20" title="20์ ๋ฏธ๋ง์ผ๋ก ์์ฑํด์ฃผ์ธ์."
pattern="^[\S]+$" placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
style="margin-top: 40px; margin-bottom: 40px; font-family: none;" /><br />
<button class="inputBtn" type="submit" value="๋ก๊ทธ์ธ">ํ์ธ</button>
</fieldset>
</form>
</div>
<a href="index.jsp" style="color: white; text-decoration: none;"><span
class="tohome"> ํ์ผ๋ก </span></a>
</div>
</body>
</html>
5. loginOk.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.io.IOException"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="pack.user.UserDAO" %>
<%@page import="pack.user.UserDTO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String password = request.getParameter("password");
UserDTO user = null;
UserDTO loginUser = new UserDTO(id, password);
UserDAO userDAO = new UserDAO();
try{
// dbํ์ธ ํ
// ๋ก๊ทธ์ธ ์ ๋ณด์ผ์น
user = userDAO.login(loginUser);
if (user != null) {
session.setAttribute("userId", id);
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("loginFail.jsp");
//response.sendRedirect("login.jsp");
}
} catch (Exception e) {
e.printStackTrace();
//response.sendRedirect("loginFail.jsp");
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
6. loginFail.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>์์ฒญ์ ์๋ตํ์ง ๋ชปํ์ต๋๋ค.</h1>
<a href="index.jsp">๋ฉ์ธ์ผ๋ก ์ด๋ํ๊ธฐ</a>
</body>
</html>
7. logout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Logout</title>
</head>
<body>
<%
if(session.getAttribute("userId") == null){
response.sendRedirect("index.jsp");
}
else{
session.removeAttribute("userId");
response.sendRedirect("index.jsp");
}
%>
</body>
</html>
8. open.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="pack.user.UserDAO" %>
<%@page import="pack.vote.VoteDAO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if(!(session.getAttribute("userId").equals("admin"))){
response.sendRedirect("index.jsp");
}
else{
UserDAO userDAO = new UserDAO();
try{
try{
int cnt = userDAO.adminOpen();
}catch(Exception e){
throw new Exception("admin open ์คํจ");
}
VoteDAO voteDAO = new VoteDAO();
try{
voteDAO.closeVote();
voteDAO.openVote();
}catch(Exception e){
throw new Exception("ํฌํ ๋ฆฌ์
์คํจ");
}
response.sendRedirect("index.jsp");
}catch(Exception e){
e.printStackTrace();
response.sendRedirect("loginFail.jsp");
}
}
%>
</body>
</html>
9. close.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="pack.user.UserDAO" %>
<%@page import="pack.vote.VoteDAO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if(!(session.getAttribute("userId").equals("admin"))){
response.sendRedirect("index.jsp");
}
else{
UserDAO userDAO = new UserDAO();
try{
int cnt = userDAO.adminClose();
}catch(Exception e){
e.printStackTrace();
response.sendRedirect("loginFail.jsp");
}
response.sendRedirect("index.jsp");
}
%>
</body>
</html>
10. radioResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="pack.user.UserDTO" %>
<%@page import="pack.user.UserDAO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if(new UserDAO().findUser(new UserDTO("admin")).getAge()==1) {
response.sendRedirect("yeunsung_resultpage.jsp");
}else{
if(session.getAttribute("userId") == null){
response.sendRedirect("index.jsp");
}else{
//session.setAttribute("userId", "ahhyun");
if(session.getAttribute("userId") != null){
request.setCharacterEncoding("UTF-8");
//String id = new String("ahhyun");
String id = (String)session.getAttribute("userId");
String menu[] = request.getParameterValues("menu");
try {
Class.forName("com.mysql.cj.jdbc.Driver"); // DB์ฐ๊ฒฐ ๊ฐ์ฒด ์์ฑ
Connection con = DriverManager.getConnection("jdbc:mysql://13.209.88.99:3306/jsp", "aws", "1234");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select id from vote_user where id='"+id+"'");
//๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋์ผํ ๋ฒํธ๊ฐ ์๋์ง ํ์ธ
if(rs.next()) {
ResultSet rs2=stmt.executeQuery("select voter from vote_result where voter='"+id+"'");
if(rs2.next()){
%>
<script>
alert("์ด๋ฏธ ํฌํ๋ฅผ ์๋ฃํ์ต๋๋ค.");
history.go(-1);
</script>
<%
response.sendRedirect("./yeunsung_resultpage.jsp");
}else{
stmt.executeUpdate("insert into vote_result (voter, candidate, vote_time) values ('"+id+"', '"+menu[0]+"', sysdate())");
%>
<span>๊ธฐ๋ก๋์์ต๋๋ค.</span>
<h1><%=id %></h1>
<h1><%=menu[0] %></h1>
<%
response.sendRedirect("./yeunsung_resultpage.jsp");
}
}
else{
%>
<script>
alert("์กด์ฌํ์ง ์๋ ID์
๋๋ค."); //
history.go(-1);
</script>
<%
}
stmt.close();
con.close();
} catch (Exception e) {
out.println(e);
}
}else{
%>
<script>
alert("๋ก๊ทธ์ธ ํด์ฃผ์ธ์"); //
history.go(-1);
</script>
<%
}
}
}
%>
</body>
</html>
11. vote.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="pack.user.UserDTO" %>
<%@page import="pack.user.UserDAO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํฌํํ๊ธฐ</title>
<style type="text/css">
@font-face {
font-family: 'BMHANNAPro';
src:
url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_seven@1.0/BMHANNAPro.woff')
format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'BMKIRANGHAERANG';
src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_one@1.0/BMKIRANGHAERANG.woff') format('woff');
font-weight: normal;
font-style: normal;
}
* {
font-family: 'BMKIRANGHAERANG';
}
html{
background-color:black;
}
#wrap {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-size: 25px;
}
h1{
color:#F5C836;
}
#vote_wrap {
width: 80%;
color:#fff;
}
.option {
margin-top: 50px;
margin-left: 200px;
margin-bottom: 50px;
font-size: 30px;
}
#button{
font-size: 20px;
width: 100px;
height: 30px;
}
#main{
background-color:#F5C836;
font-size: 15px;
width: 50px;
height: 30px;
margin-top: 50px;
}
</style>
</head>
<body>
<%
if(new UserDAO().findUser(new UserDTO("admin")).getAge()==1) {
response.sendRedirect("yeunsung_resultpage.jsp");
}else{
if(session.getAttribute("userId") == null){
response.sendRedirect("index.jsp");
}
%>
<div id="wrap">
<h1>ํฌํํ๊ธฐ</h1>
<div id="vote_wrap">
<fieldset>
<legend>์ผ์ ๋ฉ๋ด</legend>
<form action="radioResult.jsp">
<div class="option">
<label> ์นํจ ๐: <input type="radio" name="menu" value="์นํจ"
checked="checked">
</label>
</div>
<div class="option">
<label> ํผ์ ๐: <input type="radio" name="menu" value="ํผ์">
</label>
</div>
<div class="option">
<label> ๋ถ์ ๐: <input type="radio" name="menu" value="๋ถ์">
</label>
</div>
<div class="option">
<label> ํ ๐ฃ :<input type="radio" name="menu" value="ํ">
</label>
</div>
<div class="option">
<label> ๋ญ๋ฐ ๐: <input type="radio" name="menu" value="๋ญ๋ฐ">
</label>
</div>
<div class="option">
<label> ๊ณฑ์ฐฝ ๐ท๐ฎ: <input type="radio" name="menu" value="๊ณฑ์ฐฝ">
</label>
</div>
<input type="submit" value="์ ์ก" id = "button">
</form>
</fieldset>
<button onclick="location.href='index.jsp'" id = "main">๋ฉ์ธ</button>
</div>
</div>
<%
}
%>
</body>
</html>
12. yeunsung_resultpage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.io.IOException"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="pack.vote.VoteDAO" %>
<%@page import="pack.vote.VoteDTO" %>
<%@page import="java.util.LinkedHashMap" %>
<%@page import="java.util.Set" %>
<%@page import="java.util.Random" %>
<!DOCTYPE html>
<%
Random r= new Random();
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
@font-face {
font-family: 'BMHANNAPro';
src:
url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_seven@1.0/BMHANNAPro.woff')
format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'BMKIRANGHAERANG';
src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_one@1.0/BMKIRANGHAERANG.woff') format('woff');
font-weight: normal;
font-style: normal;
}
* {
font-family: 'BMKIRANGHAERANG';
}
html{
background-color:black;
}
#wrap {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-size: 25px;
}
h1{
color:#F5C836;
}
table{
color :#fff;
}
#vote_wrap {
width: 80%;
color:#fff;
}
.option {
margin-top: 50px;
margin-left: 200px;
margin-bottom: 50px;
font-size: 30px;
}
#button{
font-size: 20px;
width: 100px;
height: 30px;
}
.tb{
border: 2px solid white;
background-color: black;
border-radius: 16px;
box-shadow: inset 0 0 8px "ffcc99";
width: 700px;
height :400px;
}
tr, td{
border: 1px solid #ffffff;
}
</style>
</head>
<body>
<div id = "wrap">
<h1 >ํฌํ ๊ฒฐ๊ณผ</h1>
<%
if(session.getAttribute("userId") == null){
response.sendRedirect("index.jsp");
}else{
String[] menus={"์นํจ","ํผ์","๋ถ์","ํ","๋ญ๋ฐ","๊ณฑ์ฐฝ"};
LinkedHashMap<String, Integer> voteResult= null;
VoteDAO voteDao = new VoteDAO();
try{
voteResult = voteDao.voteCnt();
}catch(Exception e){
e.printStackTrace();
response.sendRedirect("index.jsp");
}
int sumCount=0;
for (String key : voteResult.keySet()) {
int count = voteResult.get(key);
sumCount+= count;
}
%>
<table class="tb">
<tr>
<td colspan="3" width="180"><b><%="์ด ํฌํ์ : "+sumCount+"๋ช
" %> </b></td>
<td width="60"><b><%="๋ํ์"%></b></td>
</tr>
<%
int i=0;
for (String key : voteResult.keySet()) {
i++;
System.out.println(i);
String rgb = "#"+Integer.toHexString(r.nextInt(255*255*255));
int count = voteResult.get(key);
int ratio = new Double(Math.round((double) count/sumCount*100)).intValue();
%>
<tr>
<td width="20" align = "center"><%=i%></td>
<td width="120" align = "center"><%=key %>
<td>
<table width="<%=ratio*5%>" >
<tr>
<td bgcolor="<%=rgb%>" height = "15" align="center" style="font-size:15px;"><%=ratio+"%" %></td>
</tr>
</table>
</td>
<td><%=voteResult.get(key) %></td>
</tr>
<% }
for(int j = 0; j < menus.length; j++) {
if(!voteResult.containsKey(menus[j])) {
i++;
}
%>
<tr>
<td width="20" align = "center"><%=i%></td>
<td width="120" align = "center"><%=menus[j] %>
<td>
<table width="<%=0%>">
<tr>
<td bgcolor="black" height = "15"></td>
</tr>
</table>
</td>
<td>0</td>
</tr>
<%
}
%>
</table>
<br>
<button onclick="location.href='index.jsp'">ํ์ผ๋ก</button>
</div>
<%
}
%>
</body>
</html>
13. UserDAO.jsp
package pack.user;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDAO {
private Connection conn;
private PreparedStatement psmt;
private ResultSet rs;
private int cnt;
private UserDTO user = null;
private void getConnection() throws Exception {
String driver = "com.mysql.cj.jdbc.Driver";
String db_url = "jdbc:mysql://13.209.88.99:3306/jsp";
String db_id = "aws";
String db_pw = "1234";
try {
Class.forName(driver);
conn = DriverManager.getConnection(db_url, db_id, db_pw);
System.out.println("db connected");
} catch (ClassNotFoundException | SQLException e) {
// e.printStackTrace();
throw new Exception("db connect ์คํจ์ ใ
ใ
");
}
}
private void close() throws Exception {
try {
if (rs != null) {
rs.close();
}
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
System.out.println("connection close");
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("db close ์คํจ์ ใ
ใ
");
}
}
public void join(UserDTO dto) throws Exception {
try {
getConnection();
String sql = "insert into vote_user values(?,?,?,?,?);";
psmt = conn.prepareStatement(sql);
System.out.println("join db ์์");
psmt.setString(1, dto.getId());
psmt.setString(2, dto.getPwd());
psmt.setString(3, dto.getName());
psmt.setInt(4, dto.getAge());
psmt.setString(5, dto.getGender());
cnt = psmt.executeUpdate();
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("join ์คํจ์ ใ
ใ
");
} finally {
close();
}
}
public UserDTO login(UserDTO dto) throws Exception {
getConnection();
try {
String sql = "select * from vote_user where id=? and password=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getId());
psmt.setString(2, dto.getPwd());
rs = psmt.executeQuery();
if (rs.next()) {
String id = rs.getString(1);
String pwd = rs.getString(2);
String name = rs.getString(3);
int age = rs.getInt(4);
String gender = rs.getString(5);
user = new UserDTO(id, pwd, name, age, gender);
}
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("login ์คํจ์ ใ
ใ
");
} finally {
close();
}
return user;
}
public UserDTO findUser(UserDTO dto) throws Exception {
getConnection();
try {
String sql = "select * from vote_user where id=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getId());
rs = psmt.executeQuery();
if (rs.next()) {
String id = rs.getString(1);
String pwd = rs.getString(2);
String name = rs.getString(3);
int age = rs.getInt(4);
String gender = rs.getString(5);
user = new UserDTO(id, pwd, name, age, gender);
}
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("find user ์คํจ์ ใ
ใ
");
} finally {
close();
}
return user;
}
public int adminOpen() throws Exception {
getConnection();
try {
String sql = "update vote_user set age=0 where id='admin';";
psmt = conn.prepareStatement(sql);
cnt = psmt.executeUpdate();
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("ํฌํ์์ ์คํจ์ ใ
ใ
");
} finally {
close();
}
return cnt;
}
public int adminClose() throws Exception {
getConnection();
try {
String sql = "update vote_user set age=1 where id='admin';";
psmt = conn.prepareStatement(sql);
cnt = psmt.executeUpdate();
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("ํฌํ ์ข
๋ฃ ์คํจ์ ใ
ใ
");
} finally {
close();
}
return cnt;
}
}
14. UserDTO.jsp
package pack.user;
public class UserDTO {
private String id;
private String pwd;
private String pwdc;
private String name;
private int age;
private String gender;
public UserDTO(String id, String pwd, String pwdc, String name, int age, String gender) {
super();
this.id = id;
this.pwd = pwd;
this.pwdc = pwdc;
this.name = name;
this.age = age;
this.gender = gender;
}
public UserDTO(String id, String pwd, String name, int age, String gender) {
super();
this.id = id;
this.pwd = pwd;
this.name = name;
this.age = age;
this.gender = gender;
}
public UserDTO(String id, String pwd) {
super();
this.id = id;
this.pwd = pwd;
}
public UserDTO(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getPwdc() {
return pwdc;
}
public void setPwdc(String pwdc) {
this.pwdc = pwdc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
15. VoteDAO.jsp
package pack.vote;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import pack.vote.VoteDTO;
public class VoteDAO {
private Connection conn;
private PreparedStatement psmt;
private ResultSet rs;
private int cnt;
private VoteDTO vote = null;
private void getConnection() throws Exception {
String driver = "com.mysql.cj.jdbc.Driver";
String db_url = "jdbc:mysql://13.209.88.99:3306/jsp";
String db_id = "aws";
String db_pw = "1234";
try {
Class.forName(driver);
conn = DriverManager.getConnection(db_url, db_id, db_pw);
System.out.println("vote db connected");
} catch (ClassNotFoundException | SQLException e) {
// e.printStackTrace();
throw new Exception("vote db connect ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ");
}
}
private void close() throws Exception {
try {
if (rs != null) {
rs.close();
}
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
System.out.println("vote connection close");
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("vote db close ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ");
}
}
public void toVote(VoteDTO dto) throws Exception {
try {
getConnection();
String sql = "insert into vote_result (voter, candidate) values(?,?);";
psmt = conn.prepareStatement(sql);
System.out.println("insert ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ");
psmt.setString(1, dto.getVoter());
psmt.setString(2, dto.getCandidate());
cnt = psmt.executeUpdate();
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("๏ฟฝ๏ฟฝวฅ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ");
} finally {
close();
}
}
public VoteDTO findVoter(VoteDTO dto) throws Exception {
getConnection();
try {
String sql = "select * from vote_result where voter=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getVoter());
rs = psmt.executeQuery();
if (rs.next()) {
int id = rs.getInt(1);
String voter = rs.getString(2);
String candidate = rs.getString(3);
vote = new VoteDTO(id, voter, candidate);
}
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("๏ฟฝ๏ฟฝวฅ๏ฟฝ๏ฟฝ ศฎ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ");
} finally {
close();
}
return vote;
}
public LinkedHashMap<String, Integer> voteCnt() throws Exception {
LinkedHashMap<String, Integer> result = new LinkedHashMap<>();
getConnection();
try {
String sql = "select count(*) as cnt, candidate from vote_result group by candidate order by cnt desc;";
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while (rs.next()) {
int cnt = rs.getInt(1);
String candidate = rs.getString(2);
result.put(candidate, cnt);
}
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("๏ฟฝ๏ฟฝวฅ ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝศธ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ");
} finally {
close();
}
return result;
}
public Boolean closeVote() throws Exception {
getConnection();
Boolean rsDrop = false;
try {
String sql = "drop table vote_result;";
psmt = conn.prepareStatement(sql);
rsDrop = psmt.execute();
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝฬบ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ");
} finally {
close();
}
return rsDrop;
}
public Boolean openVote() throws Exception {
getConnection();
Boolean rsDrop = false;
try {
String sql = "CREATE TABLE vote_result (\r\n"
+ " id int not null auto_increment,\r\n"
+ " voter varchar(20) not null,\r\n"
+ " candidate varchar(20) not null,\r\n"
+ " vote_time timestamp not null,\r\n"
+ " PRIMARY KEY (id));";
psmt = conn.prepareStatement(sql);
rsDrop = psmt.execute();
} catch (SQLException e) {
// e.printStackTrace();
throw new Exception("ลฉ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝฦฎ ๏ฟฝ๏ฟฝ๏ฟฝฬบ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ");
} finally {
close();
}
return rsDrop;
}
}
16. VoteDTO.jsp
package pack.vote;
public class VoteDTO {
private int id;
private String voter;
private String candidate;
public VoteDTO(int id, String voter, String candidate) {
super();
this.id = id;
this.voter = voter;
this.candidate = candidate;
}
public VoteDTO(String voter, String candidate) {
super();
this.voter = voter;
this.candidate = candidate;
}
public VoteDTO(String voter) {
super();
this.voter = voter;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getVoter() {
return voter;
}
public void setVoter(String voter) {
this.voter = voter;
}
public String getCandidate() {
return candidate;
}
public void setCandidate(String candidate) {
this.candidate = candidate;
}
}
๐ป Web
1๏ธโฃ ๋ฉ์ธ ํ์ด์ง
2๏ธโฃ ํ์๊ฐ์ ํ์ด์ง
3๏ธโฃ ๋ก๊ทธ์ธ ํ์ด์ง
4๏ธโฃ ๋ก๊ทธ์ธ ํ ๋ฉ์ธ ํ์ด์ง
5๏ธโฃ ํฌํ ํ์ด์ง
6๏ธโฃ ํฌํ ํํฉ ํ์ด์ง
'IT EDU > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JSP] Post, Get, sendRedirect๋ฅผ ์ด์ฉํ ํ์ด์ง ์ด๋ (0) | 2022.03.07 |
---|---|
JSP, HTML ๊ธฐ์ด (0) | 2022.03.03 |
Linux server Tomcat์ War ํ์ผ ๋ฐฐํฌ (0) | 2022.02.28 |
[JSP, Servlet, AWS] ํ์ ๊ฐ์ ํ์ด์ง ๋ง๋ค๊ธฐ (0) | 2022.02.28 |
Apache Tomcat ์ค์น ๋ฐ JavaSE-11(Eclipse) ์ฐ๋ (3) | 2022.02.24 |
๋๊ธ