IT EDU/JSP

[JSP] DAO, DTO๋ฅผ ํ†ตํ•œ ํˆฌํ‘œ ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„

yoonhoou 2022. 3. 4.
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๏ธโƒฃ ํˆฌํ‘œ ํ˜„ํ™ฉ ํŽ˜์ด์ง€

 

 

 

๋Œ“๊ธ€