IT EDU/JSP

[JSP, Servlet, AWS] 회원 가입 페이지 만들기

yoonhoou 2022. 2. 28.
728x90

 

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="Content-Type" content="width=device-width, initial-scale=1.0">
		<title>Insert title here</title>
		<style>
			@font-face {
			   font-family: 'SANJUGotgam';
			   src:
			      url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2112@1.0/SANJUGotgam.woff')
			      format('woff');
			   font-weight: normal;
			   font-style: normal;
			}
			
			@font-face {
			    font-family: 'HSYuji-Regular';
			    src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2108@1.1/HSYuji-Regular.woff') format('woff');
			    font-weight: normal;
			    font-style: normal;
			}
			
			* {
			   margin: 0;
			   padding: 0;
			   box-sizing: border-box;
			}
			
			.nav-menu {
			   display: flex;
			   justify-content: space-around;
			   align-items: center;
			   height: 100px;
			   background-color: #e3e3e3;
			}
			
			.nav-menu a {
			   font-family: 'SANJUGotgam';
			   font-size: 20px;
			   text-decoration: none;
			   position: relative;
			   padding: 6px 12px;
			   color: black;
			}
			
			.nav-menu a::after {
			   content: "";
			   position: absolute;
			   bottom: 0;
			   left: 50%;
			   transform: translateX(-50%);
			   width: 0;
			   height: 4px;
			   background: #40E0D0;
			   transition: all .5s ease-out;
			}
			
			.nav-menu a:hover::after {
			   width: 100%;
			}
			
			#wrap h2{
			   text-align:center;
			   font-family: 'HSYuji-Regular';
			}
			
			#group{
			
			   font-size: 50px;
			   color: #fffb19;
			   text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -2px black;
			
			}
			
			#one{
			   color: #8bab89;
			}
			
			
			#two{
			   color: #ffabf7;
			
			}
			
			#three{
			   color: #c8b9eb;
			}
			
			#four{
			   color: #bfd9ff;   
			
			}
		</style>
	</head>

	<body>
	   <nav class="nav-menu">
	      <a href="index.html">HOME</a>
	      <a href="insert.html">회원가입</a>
	      <a href="list.do">회원목록</a>
	      <a href="near_store.html">주변맛집</a>
	   </nav>
	   <br/>
	   <br/>
	   <br/>
	   <div id ="wrap" >
	         <h2 id="group">2조</h2>
	         <br/><hr/><hr/><br/>
	         <h2 id="one">코드로 말한다 남궁윤호</h2>
	         <br/>
	         <h2 id="two">14살 허예인</h2>
	         <br/>
	         <h2 id="three">하드캐리 최최최</h2>
	         <br/>
	         <h2 id="four">숙명여대 퀸카 김아현</h2>
	   </div>
	</body>
</html>

 


 

insert.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Type"
   content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>

<style type="text/css">
@font-face {
   font-family: 'SANJUGotgam';
   src:
      url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2112@1.0/SANJUGotgam.woff')
      format('woff');
   font-weight: normal;
   font-style: normal;
}

* {
   font-family: 'SANJUGotgam';
   margin: 0;
   padding: 0;
   box-sizing: border-box;
}

.nav-menu {
   display: flex;
   justify-content: space-around;
   align-items: center;
   height: 100px;
   background-color: #e3e3e3;
}

.nav-menu a {
   font-family: 'SANJUGotgam';
   font-size: 20px;
   text-decoration: none;
   position: relative;
   padding: 6px 12px;
   color: black;
}

.nav-menu a::after {
   content: "";
   position: absolute;
   bottom: 0;
   left: 50%;
   transform: translateX(-50%);
   width: 0;
   height: 4px;
   background: #40E0D0;
   transition: all .5s ease-out;
}

.nav-menu a:hover::after {
   width: 100%;
}

.input {
   width: 50%;
   height: 25px;
   border: none;
   /*    border-bottom: 1px solid black; */
   margin-top: 15px;
   margin-bottom: 15px;
   border-radius: 5px;
}

input::placeholder {
   opacity: 0.5;
   text-align: center;
}

.inputBtn {
   margin-top:15px;
   width: 140px;
   height: 30px;
   border: solid 2px white;
   border-radius: 5px;
   background-color: #5ad3c0;
   color: white;
   
}
.inputBtn:hover {
   color: orange;
   border: solid 2px orange;
}


</style>



</head>
<div>
   <nav class="nav-menu"> <a href="index.html">HOME</a> <a
      href="insert.html">회원가입</a> <a href="list.do">회원목록</a> <a href="">주변맛집</a>
   </nav>
</div>


<body style="margin: 0px;">
   <div
      style="width: 30%; height: 600px; margin-top: 80px; margin-left: 34%; background-color: #5ad3c0; 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: 50px;">
         <form method="post" action="abc" 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="pwd" required="true" maxlength="20" title="20자 미만으로 작성해주세요." pattern="^[\S]+$" placeholder="비밀번호를 입력해주세요." /><br />
               <input class="input input_pwc" type="password" name="pwdc" required="true" maxlength="20" title="20자 미만으로 작성해주세요." pattern="^[\S]+$" placeholder="비밀번호를 한번 더 입력해주세요." /><br /> 
               <input class="input input_email" type="email" name="email" required="true" maxlength="30" title="30자 미만의 이메일 주소만 사용가능합니다." pattern="^[\S]+@.+[.][\S]+$" placeholder="ex) hogildong@gmail.com" /><br />
               <input class="input input_phone" type="tel" name="phone" pattern="^[0-9]{3}-[0-9]{4}-[0-9]{4}$" title="지정된 양식을 지켜주세요." required="true" placeholder="ex) 010-1234-5678" /><br />
               <button class="inputBtn" type="submit" value="가입">가입</button>
               <button class="inputBtn" type="reset" value="취소">취소</button>
            </fieldset>
         </form>
      </div>
   </div>

</body>

</html>

 


 

near_store.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
<style>
@font-face {
   font-family: 'SANJUGotgam';
   src:
      url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2112@1.0/SANJUGotgam.woff')
      format('woff');
   font-weight: normal;
   font-style: normal;
}

@font-face {
    font-family: 'IM_Hyemin-Bold';
    src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2106@1.1/IM_Hyemin-Bold.woff2') format('woff');
    font-weight: normal;
    font-style: normal;
}

* {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
}

.nav-menu {
   display: flex;
   justify-content: space-around;
   align-items: center;
   height: 100px;
   background-color: #e3e3e3;
}

.nav-menu a {
   font-family: 'SANJUGotgam';
   font-size: 20px;
   text-decoration: none;
   position: relative;
   padding: 6px 12px;
   color: black;
}

.nav-menu a::after {
   content: "";
   position: absolute;
   bottom: 0;
   left: 50%;
   transform: translateX(-50%);
   width: 0;
   height: 4px;
   background: #40E0D0;
   transition: all .5s ease-out;
}

.nav-menu a:hover::after {
   width: 100%;
}

#wrap {
  font-family: 'IM_Hyemin-Bold';
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-top: 100px;
}


#wrap h2{
   margin-bottom: 100px;
   border-bottom: thick double #32a1ce;
}

</style>
</head>

<body>
   <nav class="nav-menu">
      <a href="index.html">HOME</a>
      <a href="insert.html">회원가입</a>
      <a href="list.do">회원목록</a>
      <a href="near_store.html">주변맛집</a>
   </nav>
   <div id = "wrap">
      <h2>우리들의 점심</h2>
      <div class = "store">
         <a href="https://map.naver.com/v5/entry/place/13149692?c=14140748.5449753,4508175.1079668,15,0,0,0,dh&placePath=%3Fentry=plt">
            <h3>초선과여포</h3>
         </a>
         
         <div class = "store_image">
            
         </div>
      </div>
      
      <div class = "store">
         <a href="https://map.naver.com/v5/search/%EC%98%A4%EB%8A%98%ED%86%B5%EB%8B%AD/place/1417398034?placePath=%3Fentry=pll%26from=nx%26fromNxList=true&c=14135496.5839705,4506189.5408103,15,0,0,0,dh">
            <h3>오늘통닭</h3>
         </a>
         
         
         <div class = "store_image">
            <img src="" />
         </div>
         
      </div>
      
      <div class = "store">
         <a href="https://map.naver.com/v5/search/%ED%95%98%EB%82%98%EC%9A%B0%EB%8F%99/place/19712875?placePath=%3Fentry=pll%26from=nx%26fromNxList=true">
            <h3>하나우동</h3>
         </a>
         
         <div class = "store_image">
            
         </div>
      
      </div>
      
      <div class = "store">
         <a href="https://map.naver.com/v5/entry/place/20767050?c=14140637.6277665,4508331.2275120,15,0,0,0,dh&placePath=%3Fentry=plt">
            <h3>한돈애</h3>
         </a>
         
         <div class = "store_image">
            
         </div>
      
      </div>
      
      <div class = "store">
         
         <a href="https://map.naver.com/v5/entry/place/1125259502?c=14140722.5219936,4508226.1694199,13,0,0,0,dh&placePath=%2Fhome%3Fentry=plt">
            <h3>살롱주막집</h3>
         </a>
         <div class = "store_image">
            
         </div>
      </div>
      

   </div>
   
   
</body>
</html>

 


 

insertServlet.java

package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InsertServlet extends HttpServlet {
   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {

      // 1. 파라미터로 전송된 값을 얻어오기.
      request.setCharacterEncoding("UTF-8");
      String id = request.getParameter("id");
      String pwd = request.getParameter("pwd");
      String pwdc = request.getParameter("pwdc");
      String email = request.getParameter("email");
      String phone = request.getParameter("phone");
      
      // navbar 생성 코드
	  
	  response.setContentType("text/html;charset=utf-8");
	  PrintWriter pw = response.getWriter();
	  
	  pw.println("<html>");
	  pw.println("<head></head>");
	  
	  pw.println("<style>"
			  + "@font-face {\r\n"
			  + "   font-family: 'SANJUGotgam';\r\n"
			  + "   src:\r\n"
			  + "      url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2112@1.0/SANJUGotgam.woff')\r\n"
			  + "      format('woff');\r\n"
			  + "   font-weight: normal;\r\n"
			  + "   font-style: normal;\r\n"
			  + "}\r\n"
			  + "\r\n"
			  + "* {\r\n"
			  + "   margin: 0;\r\n"
			  + "   padding: 0;\r\n"
			  + "   box-sizing: border-box;\r\n"
			  + "}\r\n"
			  + "\r\n"
			  + ".nav-menu {\r\n"
			  + "   display: flex;\r\n"
			  + "   justify-content: space-around;\r\n"
			  + "   align-items: center;\r\n"
			  + "   height: 100px;\r\n"
			  + "   background-color: #e3e3e3;\r\n"
			  + "}\r\n"
			  + "\r\n"
			  + ".nav-menu a {\r\n"
			  + "   font-family: 'SANJUGotgam';\r\n"
			  + "   font-size: 20px;\r\n"
			  + "   text-decoration: none;\r\n"
			  + "   position: relative;\r\n"
			  + "   padding: 6px 12px;\r\n"
			  + "   color: black;\r\n"
			  + "}\r\n"
			  + "\r\n"
			  + ".nav-menu a::after {\r\n"
			  + "   content: \"\";\r\n"
			  + "   position: absolute;\r\n"
			  + "   bottom: 0;\r\n"
			  + "   left: 50%;\r\n"
			  + "   transform: translateX(-50%);\r\n"
			  + "   width: 0;\r\n"
			  + "   height: 4px;\r\n"
			  + "   background: #40E0D0;\r\n"
			  + "   transition: all .5s ease-out;\r\n"
			  + "}\r\n"
			  + "\r\n"
			  + ".nav-menu a:hover::after {\r\n"
			  + "   width: 100%;\r\n"
			  + "}\r\n"
			  
           + "</style>");
	  // pw.println("<body>");
	  pw.println("<body>\r\n"
			  + "   <nav class=\"nav-menu\">\r\n"
			  + "      <a href=\"index.html\">HOME</a>\r\n"
			  + "      <a href=\"insert.html\">회원가입</a>\r\n"
			  + "      <a href=\"list.do\">회원목록</a>\r\n"
			  + "      <a href=\"near_store.html\">주변맛집</a>\r\n"
			  + "   </nav>\r\n"
			  + "   \r\n"
			  + "</body>\r\n");
	  
	  // 아이디 중복확인
	  Connection IDcon = null;
	  PreparedStatement IDpstmt = null;
	  int findID = 0;
	  
	  try {
		Class.forName("com.mysql.cj.jdbc.Driver");
		IDcon = DriverManager.getConnection("jdbc:mysql://54.180.125.242:3306/test?useUnicode=true&serverTimezone=Asia/Seoul", "aws", "1234");
		String IDsql = "select * from member where id=?";
	    IDpstmt = IDcon.prepareStatement(IDsql);
	  	IDpstmt.setString(1, id);
	  	System.out.println("idcheck DB접속");
	  	ResultSet rs = IDpstmt.executeQuery();
	  	findID = rs.next() ? 1 : 0; 
	  } catch (ClassNotFoundException | SQLException e) {
		e.printStackTrace();
	  } finally {
		  try {
			  if (IDpstmt != null)
				  IDpstmt.close();
			  
			  if (IDcon != null)
				  IDcon.close();
			  
		  } catch (SQLException se) {
			  System.out.println(se.getMessage());
		  } 
	  }
	  if (findID == 1) {
		  pw.println(
				  "<html>" + 
						  "<head>"+
						  "</head>" +
						  "<body>" + 
						  "<h3 style=\"font-size: 20; text-align:center; \">중복된 아이디 입니다.</h3>"+
						  "<a href='javascript:history.go(-1)' style=\"display:block; width:100%; color: black; font-size: 20; text-align:center \">이전페이지로 가기</a>" +
						  "</body>" + 
						  "</html>"
				  );
		  pw.close();
		  // 비밀번호 일치 확인
      } else if (pwd.equals(pwdc)) {
    	  int n = 0;
    	  PreparedStatement pstmt = null;
    	  Connection con = null;
    	  
    	  try {
    		  
    		  // 2. 전송된 값을 db에 저장.
    		  
    		  // 동적 클래스 로드
    		  Class.forName("com.mysql.cj.jdbc.Driver"); // DB연결 객체 생성
    		  con = DriverManager.getConnection("jdbc:mysql://54.180.125.242:3306/test?useUnicode=true&serverTimezone=Asia/Seoul", "aws", "1234");
    		  String sql = "insert into member values( ?,?,?,?, sysdate());";
    		  pstmt = con.prepareStatement(sql);
    		  System.out.println("insert DB접속");
    		  
    		  pstmt.setString(1, id);
    		  pstmt.setString(2, pwd);
    		  pstmt.setString(3, email);
    		  pstmt.setString(4, phone);
    		  
    		  // sql구문 실행하기
    		  
    		  n = pstmt.executeUpdate();
    		  
    	  } catch (ClassNotFoundException ce) {
    		  System.out.println(ce.getMessage());
    	  } catch (SQLException se) {
    		  System.out.println(se.getMessage());
    	  } finally {
    		  try {
    			  
    			  if (pstmt != null)
    				  pstmt.close();
    			  
    			  if (con != null)
    				  con.close();
    			  
    		  } catch (SQLException se) {
    			  System.out.println(se.getMessage());
    		  }
    	  }
    	  
    	  // 3. 사용자(클라이언트)에 결과를 응답하기.

    	  if (n > 0) {
    		  pw.println(
    				  "<html>" + 
    						  "<head>"+
    						  "</head>" +
    						  "<body>" + 
    						  "<h3 style=\"font-size: 20; text-align:center; \">가입에 성공했습니다.</h3>"+
    						  "<a href='index.html' style=\"display:block; width:100%; color: black; font-size: 20; text-align:center \">홈으로 가기</a>" +
    						  "</body>" + 
    						  "</html>"
    				  );
    		  pw.close();
    	  } else {
    		  pw.println(
    				  "<html>" + 
    						  "<head>"+
    						  "</head>" +
    						  "<body>" + 
    						  "<h3 style=\"font-size: 20; text-align:center; \">가입에 실패했습니다.</h3>"+
    						  "<a href='javascript:history.go(-1)' style=\"display:block; width:100%; color: black; font-size: 20; text-align:center \">이전페이지로 가기</a>" +
    						  "</body>" + 
    						  "</html>"
    				  );
    		  pw.close();
    	  }
      } else {
		  pw.println(
				  "<html>" + 
						  "<head>"+
						  "</head>" +
						  "<body>" + 
						  "<h3 style=\"font-size: 20; text-align:center; \">비밀번호가 일치하지 않습니다.</h3>"+
						  "<a href='javascript:history.go(-1)' style=\"display:block; width:100%; color: black; font-size: 20; text-align:center \">이전페이지로 가기</a>" +
						  "</body>" + 
						  "</html>"
				  );
		  pw.close();
      }
   }
}

 


 

DeleteServlet.java

package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteServlet extends HttpServlet {
   @Override
   protected void service(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      Connection con = null;
      PreparedStatement pstmt = null;
      int n = 0;
      
      request.setCharacterEncoding("UTF-8");
      String id = request.getParameter("id");

      response.setContentType("text/html;charset=UTF-8");
      PrintWriter pw = response.getWriter();
       pw.println("<html>");
         pw.println("<head></head>");
         pw.println("<style>"
               + "@font-face {\r\n"
               + "   font-family: 'SANJUGotgam';\r\n"
               + "   src:\r\n"
               + "      url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2112@1.0/SANJUGotgam.woff')\r\n"
               + "      format('woff');\r\n"
               + "   font-weight: normal;\r\n"
               + "   font-style: normal;\r\n"
               + "}\r\n"
               + "\r\n"
               + "* {\r\n"
               + "   margin: 0;\r\n"
               + "   padding: 0;\r\n"
               + "   box-sizing: border-box;\r\n"
               + "}\r\n"
               + "\r\n"
               + ".nav-menu {\r\n"
               + "   display: flex;\r\n"
               + "   justify-content: space-around;\r\n"
               + "   align-items: center;\r\n"
               + "   height: 100px;\r\n"
               + "   background-color: #e3e3e3;\r\n"
               + "}\r\n"
               + "\r\n"
               + ".nav-menu a {\r\n"
               + "   font-family: 'SANJUGotgam';\r\n"
               + "   font-size: 20px;\r\n"
               + "   text-decoration: none;\r\n"
               + "   position: relative;\r\n"
               + "   padding: 6px 12px;\r\n"
               + "   color: black;\r\n"
               + "}\r\n"
               + "\r\n"
               + ".nav-menu a::after {\r\n"
               + "   content: \"\";\r\n"
               + "   position: absolute;\r\n"
               + "   bottom: 0;\r\n"
               + "   left: 50%;\r\n"
               + "   transform: translateX(-50%);\r\n"
               + "   width: 0;\r\n"
               + "   height: 4px;\r\n"
               + "   background: #40E0D0;\r\n"
               + "   transition: all .5s ease-out;\r\n"
               + "}\r\n"
               + "\r\n"
               + ".nav-menu a:hover::after {\r\n"
               + "   width: 100%;\r\n"
               + "}\r\n"
               + "</style>");
         pw.println("<body>");
         pw.println("<body>\r\n"
                  + "   <nav class=\"nav-menu\">\r\n"
                  + "      <a href=\"index.html\">HOME</a>\r\n"
                  + "      <a href=\"insert.html\">회원가입</a>\r\n"
                  + "      <a href=\"list.do\">회원목록</a>\r\n"
                  + "      <a href=\"near_store.html\">주변맛집</a>\r\n"
                  + "   </nav>\r\n"
                  + "   \r\n"
                  + "</body>\r\n");
      try {
         Class.forName("com.mysql.cj.jdbc.Driver"); // DB연결 객체 생성
         con = DriverManager.getConnection("jdbc:mysql://54.180.125.242:3306/test?useUnicode=true&serverTimezone=Asia/Seoul", "aws", "1234");
         String sql = "delete from member where id=?";
         pstmt = con.prepareStatement(sql);
         System.out.println("delete DB접속");
         pstmt.setString(1, id);
         n = pstmt.executeUpdate();
      } catch (ClassNotFoundException | SQLException e) {
         System.out.println(e.getMessage());
      } finally {
         try {
            if (pstmt != null)
               pstmt.close();
            if (con != null)
               con.close();
         } catch (SQLException se) {
            System.out.println(se.getMessage());
         }
      }

      if (n > 0) {
         pw.println(
               "<html>" + 
                     "<head>"+
                     "</head>" +
                     "<body>" + 
                        "<h3 style=\"font-size: 20; text-align:center; \">삭제에 성공했습니다.</h3>"+
                        "<a href='list.do' style=\"display:block; width:100%; color: black; font-size: 20; text-align:center \">회원목록으로 돌아가기</a>" +
                     "</body>" + 
               "</html>"
         );
         pw.close();
      } else {
         pw.println(
               "<html>" + 
                     "<head>"+
                     "</head>" +
                     "<body>" + 
                        "<h3 style=\"font-size: 20; text-align:center; \">삭제에 실패했습니다.</h3>"+
                        "<a href='javascript:history.go(-1)' style=\"display:block; width:100%; color: black; font-size: 20; text-align:center \">이전페이지로 가기</a>" +
                     "</body>" + 
               "</html>"
         );
         pw.close();
      }

   }

}

 


 

UpdateServlet.java

package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateServlet extends HttpServlet {
   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      // 1. parameter로 전송된 id얻기.
      String id = req.getParameter("id");
      
      resp.setContentType("text/html;charset=UTF-8");
      PrintWriter pw = resp.getWriter();
      pw.println("<html>");
      pw.println("<head>"
            +"<style type=\"text/css\">\r\n"
            + "@font-face {\r\n"
            + "   font-family: 'SANJUGotgam';\r\n"
            + "   src:\r\n"
            + "      url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2112@1.0/SANJUGotgam.woff')\r\n"
            + "      format('woff');\r\n"
            + "   font-weight: normal;\r\n"
            + "   font-style: normal;\r\n"
            + "}\r\n"
            + "\r\n"
            + "* {\r\n"
            + "   font-family: 'SANJUGotgam';\r\n"
            + "   margin: 0;\r\n"
            + "   padding: 0;\r\n"
            + "   box-sizing: border-box;\r\n"
            + "}\r\n"
            + "\r\n"
            
            + ".input {\r\n"
            + "   width: 50%;\r\n"
            + "   height: 25px;\r\n"
            + "   border: none;\r\n"
            + "   /*    border-bottom: 1px solid black; */\r\n"
            + "   margin-top: 15px;\r\n"
            + "   margin-bottom: 15px;\r\n"
            + "   border-radius: 5px;\r\n"
            + "}\r\n"
            + "\r\n"
            + "input::placeholder {\r\n"
            + "   opacity: 0.5;\r\n"
            + "   text-align: center;\r\n"
            + "}\r\n"
            + "\r\n"
            + ".inputBtn {\r\n"
            + "   margin-top:15px;\r\n"
            + "   width: 140px;\r\n"
            + "   height: 30px;\r\n"
            + "   border: solid 2px white;\r\n"
            + "   border-radius: 5px;\r\n"
            + "   background-color: #5ad3c0;\r\n"
            + "   color: white;\r\n"
            + "   \r\n"
            + "}\r\n"
            + ".inputBtn:hover {\r\n"
            + "   color: orange;\r\n"
            + "   border: solid 2px orange;\r\n"
            + "}\r\n"
            + "\r\n"
            + "\r\n"
            + "</style>"
            + "</head>");
      pw.println("<body>");

      PreparedStatement pstmt = null;
      Connection con = null;
      ResultSet rs = null;

      try {
         Class.forName("com.mysql.cj.jdbc.Driver"); // DB연결 객체 생성
         con = DriverManager.getConnection("jdbc:mysql://54.180.125.242:3306/test?useUnicode=true&serverTimezone=Asia/Seoul", "aws", "1234");
         String sql = "select * from member where id=?";
         pstmt = con.prepareStatement(sql);
         pstmt.setString(1, id);
         System.out.println("update 하기전 수정할 정보를 가져오기 위한 select DB접속");

         // sql구문 실행하기
         rs = pstmt.executeQuery();
         rs.next();
         String pwd = rs.getString("pwd");
         String email = rs.getString("email");
         String phone = rs.getString("phone");
         pw.println("<br/><br/><br/>");
         pw.println("<div style=\"padding-bottom: 50px; margin-top: 50px;\">");
         pw.println("<form method='post' action='updateok.do'>");
         pw.println("<fieldset id=\"regbox\" style=\"text-align: center; border: none;\"><br/><br/>");
         pw.println("<input type='hidden' name='id' value='" + id + "'/>");		
         pw.println("아이디<input type='text' name='id' value='" + id + "' disabled='disabled'/><br/><br/>");
         pw.println("비밀번호<input type='password' name='pwd' required=\"true\" maxlength=\"20\" title=\"20자 미만으로 작성해주세요.\" pattern=\"^[\\S]+$\" placeholder=\"새로운 비밀번호를 입력해주세요.\" value='" + pwd + "'/><br/><br/>");
         pw.println("email<input type='email' name='email' required=\"true\" maxlength=\"30\" title=\"30자 미만의 이메일 주소만 사용가능합니다.\" pattern=\"^[\\S]+@.+[.][\\S]+$\" placeholder=\""+ email +"\" value='" + email + "'/><br/><br/>");
         pw.println("phone<input type='tel' name='phone' pattern=\"^[0-9]{3}-[0-9]{4}-[0-9]{4}$\" title=\"지정된 양식을 지켜주세요.\" required=\"true\" placeholder=\"" + phone + "\" value='" + phone + "'/><br/><br/>");
         pw.println("<input class=\"inputBtn\" type='submit' value='저장'/><br/>");
         pw.println("</fieldset>");
         pw.println("</form>");
         pw.println("</div>");

      } catch (ClassNotFoundException | SQLException ce) {
         System.out.println(ce.getMessage());
		  pw.println(
				  "<html>" + 
						  "<head>"+
						  "</head>" +
						  "<body>" + 
						  "<h3 style=\"font-size: 20; text-align:center; \">회원 정보를 불러오지 못했습니다.</h3><br/>"+
						  "<a href='index.html' style=\"display:block; width:100%; color: black; font-size: 20; text-align:center \">홈으로</a>" +
						  "</body>" + 
						  "</html>"
				  );
      } finally {
    	  try {
            if (rs != null)
               rs.close();
            if (pstmt != null)
               pstmt.close();
            if (con != null)
               con.close();
         } catch (SQLException se) {
            System.out.println(se.getMessage());
         }
         pw.println("</body>");
         pw.println("</html>");
         pw.close();
      }

   }

}

 


 

UpdateOkServlet

package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateOkServlet extends HttpServlet {

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
	   req.setCharacterEncoding("UTF-8");
      String id = req.getParameter("id");
      String pwd = req.getParameter("pwd");
      String email = req.getParameter("email");
      String phone = req.getParameter("phone");

      PreparedStatement pstmt = null;
      Connection con = null;
      int n = 0;
      resp.setContentType("text/html;charset=UTF-8");
      PrintWriter pw = resp.getWriter();

      try {

         Class.forName("com.mysql.cj.jdbc.Driver"); // DB연결 객체 생성
         con = DriverManager.getConnection("jdbc:mysql://54.180.125.242:3306/test?useUnicode=true&serverTimezone=Asia/Seoul", "aws", "1234");
         String sql = "update member set pwd=?,email=?,phone=? where id=?";
         pstmt = con.prepareStatement(sql);
         pstmt.setString(1, pwd);
         pstmt.setString(2, email);
         pstmt.setString(3, phone);
         pstmt.setString(4, id);

         n = pstmt.executeUpdate();

         if (n > 0) {

            resp.sendRedirect("list.do");

         } else {
        	throw new Exception();


         }

      } catch (Exception e) {
         System.out.println(e.getMessage());
         pw.println(
                 "<html>" + 
                       "<head>"+
                       "</head>" +
                       "<body>" + 
                          "<h3 style=\"font-size: 20; text-align:center; \">정보 수정에 실패했습니다.</h3>"+
                          "<a href='index.html' style=\"display:block; width:100%; color: black; font-size: 20; text-align:center \">홈으로 가기</a>" +
                       "</body>" + 
                 "</html>"
           );
      } finally {
    	  try {
    		  if (pstmt != null)
    			  pstmt.close();
    		  if (con != null)
    			  con.close();
    	  } catch (SQLException se) {
    		  System.out.println(se.getMessage());
    	  }
          pw.close();
      }

   }

}

 


 

ListServlet.java

package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListServlet extends HttpServlet {
   @Override
   protected void service(HttpServletRequest resquest, HttpServletResponse response)
         throws ServletException, IOException {
      Connection con = null;
      PreparedStatement pstmt = null;
      ResultSet rs = null;
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter pw = response.getWriter();

      pw.println("<html>");
      pw.println("<head>");
      pw.println("<style>"
            + "table {"
            + "  border-collapse: collapse;"
            + "  width: 100%;"
            + "}"
            + "th, td {"
            + "  padding: 3px;"
            + "  text-align: center;"
            + "  border: 1px solid #DDD;"
            + "}"
            + "tr:hover {"
            + "background-color: #5AD3C0;"
            + "color: black;"
            + "}"
            + "a {"
            + "color: black;"
            + "}"
            
            + "@font-face {\r\n"
            + "   font-family: 'SANJUGotgam';\r\n"
            + "   src:\r\n"
            + "      url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2112@1.0/SANJUGotgam.woff')\r\n"
            + "      format('woff');\r\n"
            + "   font-weight: normal;\r\n"
            + "   font-style: normal;\r\n"
            + "}\r\n"
            + "\r\n"
            + "* {\r\n"
            + "   margin: 0;\r\n"
            + "   padding: 0;\r\n"
            + "   box-sizing: border-box;\r\n"
            + "}\r\n"
            + "\r\n"
            + ".nav-menu {\r\n"
            + "   display: flex;\r\n"
            + "   justify-content: space-around;\r\n"
            + "   align-items: center;\r\n"
            + "   height: 100px;\r\n"
            + "   background-color: #e3e3e3;\r\n"
            + "}\r\n"
            + "\r\n"
            + ".nav-menu a {\r\n"
            + "   font-family: 'SANJUGotgam';\r\n"
            + "   font-size: 20px;\r\n"
            + "   text-decoration: none;\r\n"
            + "   position: relative;\r\n"
            + "   padding: 6px 12px;\r\n"
            + "   color: black;\r\n"
            + "}\r\n"
            + "\r\n"
            + ".nav-menu a::after {\r\n"
            + "   content: \"\";\r\n"
            + "   position: absolute;\r\n"
            + "   bottom: 0;\r\n"
            + "   left: 50%;\r\n"
            + "   transform: translateX(-50%);\r\n"
            + "   width: 0;\r\n"
            + "   height: 4px;\r\n"
            + "   background: #40E0D0;\r\n"
            + "   transition: all .5s ease-out;\r\n"
            + "}\r\n"
            + "\r\n"
            + ".nav-menu a:hover::after {\r\n"
            + "   width: 100%;\r\n"
            + "}\r\n"
            
            + "</style>");
      pw.println("</head>");
      pw.println("<body>");
      
      pw.println("<body>\r\n"
               + "   <nav class=\"nav-menu\">\r\n"
               + "      <a href=\"index.html\">HOME</a>\r\n"
               + "      <a href=\"insert.html\">회원가입</a>\r\n"
               + "      <a href=\"list.do\">회원목록</a>\r\n"
               + "      <a href=\"near_store.html\">주변맛집</a>\r\n"
               + "   </nav>\r\n"
               + "   \r\n"
               + "</body>\r\n");



      try {

         Class.forName("com.mysql.cj.jdbc.Driver"); // DB연결 객체 생성
         con = DriverManager.getConnection("jdbc:mysql://54.180.125.242:3306/test?useUnicode=true&serverTimezone=Asia/Seoul", "aws", "1234");
         String sql = "select * from member;";
         pstmt = con.prepareStatement(sql);
         System.out.println("list DB접속");

         rs = pstmt.executeQuery();

         pw.println("<div>");
         pw.println("<br>");
         pw.println("<br>");
         pw.println("<table>");
         pw.println("<tr style=\"cursor: default; font-weight:bold; background-color:#5AD3C0\">");
         pw.println("<td>아이디</td>");
         pw.println("<td>비밀번호</td>");
         pw.println("<td>이메일</td>");
         pw.println("<td>전화번호</td>");
         pw.println("<td>등록일</td>");
         pw.println("<td>삭제</td>");
         pw.println("<td>수정</td>");
         pw.println("</tr>");

         while (rs.next()) {

            String id = rs.getString("id");
            String pwd = rs.getString("pwd");
            String email = rs.getString("email");
            String phone = rs.getString("phone");
            Timestamp regdate = rs.getTimestamp("regdate");

            pw.println("<tr>");
            pw.println("<td>" + id + "</td>");
            pw.println("<td>" + pwd + "</td>");
            pw.println("<td>" + email + "</td>");
            pw.println("<td>" + phone + "</td>");
            pw.println("<td>" + regdate + "</td>");
            pw.println("<td><a href='delete.do?id=" + id + "'>삭제</a></td>");
            pw.println("<td><a href='update.do?id=" + id + "'>수정</a></td>");
            pw.println("</tr>");

         }

         pw.println("</table>");
         pw.println("</div>");
         pw.println("<a href='index.html' style=\"text-aglign: center; font-weight:bold; "
               + "display:block; padding: 15px; width: 100%; text-align:center\">메인페이지로 이동</a>");

      } catch (ClassNotFoundException | SQLException e) {
         System.out.println(e.getMessage());
		  pw.println(
				  "<html>" + 
						  "<head>"+
						  "</head>" +
						  "<body>" + 
						  "<h3 style=\"font-size: 20; text-align:center; \">회원 목록을 불러오지 못했습니다.</h3>"+
						  "</body>" + 
						  "</html>"
				  );
      } finally {

         try {

            if (rs != null)
               rs.close();

            if (pstmt != null)
               pstmt.close();

            if (con != null)
               con.close();

         } catch (SQLException se) {

            System.out.println(se.getMessage());

         }

      }

      pw.println("</body>");

      pw.println("</html>");
      pw.close();
   }

}

 

댓글