안효원 안효원 2021-08-19
JSPDay5End
@a342cbd3e9792aed7f3152b8f35c26cd398710e2
202108/.settings/org.eclipse.wst.common.component
--- 202108/.settings/org.eclipse.wst.common.component
+++ 202108/.settings/org.eclipse.wst.common.component
@@ -1,8 +1,15 @@
 <?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
+        
     <wb-module deploy-name="202108">
+                
         <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
+                
         <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
+                
         <property name="context-root" value="202108"/>
+                
         <property name="java-output-path" value="/202108/build/classes"/>
+            
     </wb-module>
+    
 </project-modules>
 
202108/src/main/java/cart/AddServer.java (added)
+++ 202108/src/main/java/cart/AddServer.java
@@ -0,0 +1,64 @@
+package cart;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Servlet implementation class AddServer
+ */
+@WebServlet("/AddServer")
+public class AddServer extends HttpServlet {
+	private static final long serialVersionUID = 1L;
+       
+    /**
+     * @see HttpServlet#HttpServlet()
+     */
+    public AddServer() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+	/**
+	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+			
+		doPost(request, response);
+			
+	}
+
+	/**
+	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		
+		request.setCharacterEncoding("UTF-8");
+		response.setContentType("text/html; charset=utf-8");
+		
+		String item = request.getParameter("product");
+		int no = Integer.parseInt(request.getParameter("no"));
+		
+		CartDAO dao = new CartDAO();
+		dao.setCart(no, item);
+		PrintWriter out = response.getWriter();
+		out.println("<script>alert('The item was added!')</script>");
+		out.println("<script>history.go(-1)</script>");
+		
+		
+	}
+
+}
+
+
+
+
+
+
+
+
 
202108/src/main/java/cart/CartDAO.java (added)
+++ 202108/src/main/java/cart/CartDAO.java
@@ -0,0 +1,67 @@
+package cart;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+public class CartDAO {
+
+				// 데이터베이스 연결 관련 변수 선언
+				Connection conn = null;   //프로그램과 데이터베이스를 연결
+				PreparedStatement pstmt = null;	  //    //쿼리를 실행
+				ResultSet rs = null;	          //   쿼리 실행결과를 담음
+				
+				// 데이터베이스 연결관련 정보를 문자열로 선언
+				String driver = "com.mysql.jdbc.Driver";
+				String url = "jdbc:mysql://db.asin21.com:3306/db_demo07";
+				
+				
+				public void conn() {
+					try {
+						Class.forName(driver);
+						conn = DriverManager.getConnection(url, "demo07", "demo07!");
+						System.out.println("Database Connect Success!!");
+					} catch (Exception e) {
+						e.printStackTrace();
+						System.out.println("Database Connect Fail!!");
+					}
+					
+				}
+				public void disconn() {
+					try {
+						if(rs != null) {
+							rs.close();
+						}
+						if(pstmt != null) {
+							pstmt.close();
+						}
+						if(conn != null) {
+							conn.close();
+						}
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
+				}
+				
+				public void setCart(int member_no, String product_name) {
+					conn();
+					
+					try {
+						pstmt = conn.prepareStatement("insert into cart values(null, ?, ?)");
+						pstmt.setInt(1, member_no);
+						pstmt.setString(2, product_name);
+						pstmt.executeUpdate();
+						
+						
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
+					disconn();
+				}
+		
+}
+
+
+
+
 
202108/src/main/java/cart/CartDTO.java (added)
+++ 202108/src/main/java/cart/CartDTO.java
@@ -0,0 +1,29 @@
+package cart;
+
+public class CartDTO {
+	
+	int no;
+	int member_no;
+	String product_name;
+	public int getNo() {
+		return no;
+	}
+	public void setNo(int no) {
+		this.no = no;
+	}
+	
+	public int getMember_no() {
+		return member_no;
+	}
+	public void setMember_no(int member_no) {
+		this.member_no = member_no;
+	}
+	
+	public String getProduct_name() {
+		return product_name;
+	}
+	public void setProduct_name(String product_name) {
+		this.product_name = product_name;
+	}
+
+}
 
202108/src/main/java/cart/LoginDAO.java (added)
+++ 202108/src/main/java/cart/LoginDAO.java
@@ -0,0 +1,101 @@
+package cart;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+public class LoginDAO {
+
+	// 데이터베이스 연결 관련 변수 선언
+		Connection conn = null;   //프로그램과 데이터베이스를 연결
+		PreparedStatement pstmt = null;	  //    //쿼리를 실행
+		ResultSet rs = null;	          //   쿼리 실행결과를 담음
+		
+		// 데이터베이스 연결관련 정보를 문자열로 선언
+		String driver = "com.mysql.jdbc.Driver";
+		String url = "jdbc:mysql://db.asin21.com:3306/db_demo07";
+		
+		
+		public void conn() {
+			try {
+				Class.forName(driver);
+				conn = DriverManager.getConnection(url, "demo07", "demo07!");
+				System.out.println("Database Connect Success!!");
+			} catch (Exception e) {
+				e.printStackTrace();
+				System.out.println("Database Connect Fail!!");
+			}
+			
+		}
+		public void disconn() {
+			try {
+				if(rs != null) {
+					rs.close();
+				}
+				if(pstmt != null) {
+					pstmt.close();
+				}
+				if(conn != null) {
+					conn.close();
+				}
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		
+		//login function
+		public boolean getLogin(String userid, String userpw) {
+			LoginDTO login = new LoginDTO();
+			conn();
+			
+			try {
+				pstmt = conn.prepareStatement("select * from member where id=? and pw=?");
+				pstmt.setString(1, userid);
+				pstmt.setString(2, userpw);
+				rs = pstmt.executeQuery();
+				rs.next();
+				
+				login.setNo(rs.getInt("no"));
+				login.setId(rs.getString("id"));
+				login.setPw(rs.getString("pw"));
+				return true;
+				
+			} catch (Exception e) {
+				System.out.println(e);
+				disconn();
+				return false;
+			}
+			
+		}
+		
+		//getting no function
+		public int getMemberNo(String userid, String userpw) {
+			int result = 0;
+			conn();
+			
+			try {
+				pstmt = conn.prepareStatement("select no from member where id=? and pw=?");
+				pstmt.setString(1, userid);
+				pstmt.setString(2, userpw);
+				rs = pstmt.executeQuery();
+				rs.next();
+				result = rs.getInt("no");
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+			disconn();
+			return result;
+		}
+		
+}
+
+
+
+
+
+
+
+
+
+
 
202108/src/main/java/cart/LoginDTO.java (added)
+++ 202108/src/main/java/cart/LoginDTO.java
@@ -0,0 +1,31 @@
+package cart;
+
+public class LoginDTO {
+	
+	int no;
+	String id;
+	String pw;
+	public int getNo() {
+		return no;
+	}
+	public void setNo(int no) {
+		this.no = no;
+	}
+	
+	public String getId() {
+		return id;
+	}
+	public void setId(String id) {
+		this.id = id;
+	}
+	
+	public String getPw() {
+		return pw;
+	}
+	public void setPw(String pw) {
+		this.pw = pw;
+	}
+	
+	
+	
+}
 
202108/src/main/java/cart/LoginServer.java (added)
+++ 202108/src/main/java/cart/LoginServer.java
@@ -0,0 +1,73 @@
+package cart;
+
+import java.io.IOException;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+/**
+ * Servlet implementation class LoginServer
+ */
+@WebServlet("/LoginServer")
+public class LoginServer extends HttpServlet {
+	private static final long serialVersionUID = 1L;
+       
+    /**
+     * @see HttpServlet#HttpServlet()
+     */
+    public LoginServer() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+	/**
+	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		
+		doPost(request, response);
+		
+	}
+
+	/**
+	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		
+		request.setCharacterEncoding("UTF-8");
+		LoginDTO login = new LoginDTO();
+		boolean result;
+		
+		String userid = request.getParameter("userid");
+		String userpw = request.getParameter("userpw");
+		
+		LoginDAO dao = new LoginDAO();
+		result= dao.getLogin(userid, userpw);
+		
+		int userNo = dao.getMemberNo(userid, userpw);
+		
+		if(result) {//login success
+			response.sendRedirect("cart/selProduct.jsp");
+			HttpSession session = request.getSession();
+			session.setAttribute("no", userNo);
+		}else {
+			response.sendRedirect("cart/login.jsp");
+		}
+	}
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
 
202108/src/main/java/cart/ProductDAO.java (added)
+++ 202108/src/main/java/cart/ProductDAO.java
@@ -0,0 +1,93 @@
+package cart;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+
+import javax.print.attribute.standard.PrinterState;
+
+public class ProductDAO {
+	
+	// 데이터베이스 연결 관련 변수 선언
+			Connection conn = null;   //프로그램과 데이터베이스를 연결
+			PreparedStatement pstmt = null;	  //    //쿼리를 실행
+			ResultSet rs = null;	          //   쿼리 실행결과를 담음
+			
+			// 데이터베이스 연결관련 정보를 문자열로 선언
+			String driver = "com.mysql.jdbc.Driver";
+			String url = "jdbc:mysql://db.asin21.com:3306/db_demo07";
+			
+			
+			public void conn() {
+				try {
+					Class.forName(driver);
+					conn = DriverManager.getConnection(url, "demo07", "demo07!");
+					System.out.println("Database Connect Success!!");
+				} catch (Exception e) {
+					e.printStackTrace();
+					System.out.println("Database Connect Fail!!");
+				}
+				
+			}
+			public void disconn() {
+				try {
+					if(rs != null) {
+						rs.close();
+					}
+					if(pstmt != null) {
+						pstmt.close();
+					}
+					if(conn != null) {
+						conn.close();
+					}
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+			}
+			
+			public ArrayList<ProductDTO> getProductList(){
+				ArrayList<ProductDTO> list = new ArrayList<ProductDTO>();
+				conn();
+				
+				try {
+					pstmt = conn.prepareStatement("select * from product");
+					rs = pstmt.executeQuery();
+					
+					while(rs.next()) {
+						ProductDTO product = new ProductDTO();
+						product.setNo(rs.getInt("no"));
+						product.setName(rs.getString("name"));
+						product.setPrice(rs.getString("price"));
+						
+						list.add(product);
+					}
+					
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+				disconn();
+				return list;
+			}
+			
+			
+			
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 
202108/src/main/java/cart/ProductDTO.java (added)
+++ 202108/src/main/java/cart/ProductDTO.java
@@ -0,0 +1,31 @@
+package cart;
+
+public class ProductDTO {
+
+	int no;
+	String name;
+	String price;
+	
+	public int getNo() {
+		return no;
+	}
+	public void setNo(int no) {
+		this.no = no;
+	}
+	
+	public String getName() {
+		return name;
+	}
+	public void setName(String name) {
+		this.name = name;
+	}
+	
+	public String getPrice() {
+		return price;
+	}
+	public void setPrice(String price) {
+		this.price = price;
+	}
+	
+		
+}
202108/src/main/java/study/DB.java
--- 202108/src/main/java/study/DB.java
+++ 202108/src/main/java/study/DB.java
@@ -8,9 +8,9 @@
 public class DB {
 	
 	// 데이터베이스 연결 관련 변수 선언
-	Connection conn = null;
-	PreparedStatement pstmt = null;
-	ResultSet ps = null;
+	Connection conn = null;   //프로그램과 데이터베이스를 연결
+	PreparedStatement pstmt = null;	  //    //쿼리를 실행
+	ResultSet rs = null;	          //   쿼리 실행결과를 담음
 	
 	// 데이터베이스 연결관련 정보를 문자열로 선언
 	String driver = "com.mysql.jdbc.Driver";
@@ -28,5 +28,42 @@
 		}
 		
 	}
+	public void disconn() {
+		try {
+			if(rs != null) {
+				rs.close();
+			}
+			if(pstmt != null) {
+				pstmt.close();
+			}
+			if(conn != null) {
+				conn.close();
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+	
+	//select Test
+	public String getLogin() {
+		conn();
+		String id = "";
+		try {
+			pstmt = conn.prepareStatement("select id from login");
+			rs = pstmt.executeQuery();
+			rs.next();
+			id = rs.getString("id");
+			
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		disconn();
+		return id;
+	}
 
 }
+
+
+
+
+
202108/src/main/java/study/DBTest.java
--- 202108/src/main/java/study/DBTest.java
+++ 202108/src/main/java/study/DBTest.java
@@ -28,7 +28,7 @@
 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 		
 		DB db = new DB();
-		db.conn();
+		System.out.println(db.getLogin());
 		
 	}
 
 
202108/src/main/webapp/cart/login.jsp (added)
+++ 202108/src/main/webapp/cart/login.jsp
@@ -0,0 +1,17 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+    pageEncoding="UTF-8"%>
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8">
+<title>Login</title>
+</head>
+<body>
+	<h2><em style="color: red;">Login</em></h2>
+	<form action="/202108/LoginServer" method="post">
+		ID : <input type="text" name="userid"> <br />
+		P/W : <input type="password" name="userpw"> <br />
+		<input type="submit" value="Login">
+	</form>
+</body>
+</html>(No newline at end of file)
 
202108/src/main/webapp/cart/selProduct.jsp (added)
+++ 202108/src/main/webapp/cart/selProduct.jsp
@@ -0,0 +1,39 @@
+<%@page import="cart.ProductDTO"%>
+<%@page import="java.util.ArrayList"%>
+<%@page import="cart.ProductDAO"%>
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+    pageEncoding="UTF-8"%>
+<%
+	request.setCharacterEncoding("UTF-8");
+	ProductDAO dao = new ProductDAO();
+	ArrayList<ProductDTO> list = new ArrayList<ProductDTO>();
+	list = dao.getProductList();
+%>
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8">
+<title>Product List</title>
+<style>
+	#page {width: 720px; margin: 0 auto;}
+</style>
+</head>
+<body>
+	<div id="page">
+		<h2>Select Product</h2>
+		<hr />
+		<form action="/202108/AddServer" method="post">
+			<input type="hidden" value="<%=session.getAttribute("no") %>" name="no">
+			<select name="product">
+				<%
+					for(ProductDTO item : list){
+						out.println("<option>" + item.getName() + "</option>");
+					}
+				%>
+			</select>
+			<input type="submit" value="add Cart">
+		</form>
+		<a href="check.jsp">Calculating</a>
+	</div>
+</body>
+</html>(No newline at end of file)
Add a comment
List