안효원 안효원 2021-08-19
JSPDay6End
@2482d3025db04a4532763f9322f27ca9709c2dba
WebTest/.classpath
--- WebTest/.classpath
+++ WebTest/.classpath
@@ -1,13 +1,33 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
-	<classpathentry kind="src" path="src/main/java"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
 	<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v9.0">
 		<attributes>
 			<attribute name="owner.project.facets" value="jst.web"/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
-	<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
-	<classpathentry kind="output" path="build/classes"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+			<attribute name="test" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+			<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
 </classpath>
WebTest/.gitignore
--- WebTest/.gitignore
+++ WebTest/.gitignore
@@ -1,1 +1,2 @@
 /build/
+/target/
WebTest/.project
--- WebTest/.project
+++ WebTest/.project
@@ -20,8 +20,14 @@
 			<arguments>
 			</arguments>
 		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
 	</buildSpec>
 	<natures>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
 		<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
 		<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
 		<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
WebTest/.settings/org.eclipse.jdt.core.prefs
--- WebTest/.settings/org.eclipse.jdt.core.prefs
+++ WebTest/.settings/org.eclipse.jdt.core.prefs
@@ -3,5 +3,9 @@
 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
 org.eclipse.jdt.core.compiler.compliance=1.8
 org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
 org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
+org.eclipse.jdt.core.compiler.release=disabled
 org.eclipse.jdt.core.compiler.source=1.8
 
WebTest/.settings/org.eclipse.m2e.core.prefs (added)
+++ WebTest/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1
WebTest/.settings/org.eclipse.wst.common.component
--- WebTest/.settings/org.eclipse.wst.common.component
+++ WebTest/.settings/org.eclipse.wst.common.component
@@ -1,14 +1,22 @@
 <?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
-        
-    <wb-module deploy-name="WebTest">
-                
-        <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="java-output-path" value="/WebTest/build/classes"/>
-        <property name="context-root" value="WebTest"/>
             
-    </wb-module>
     
+    <wb-module deploy-name="WebTest-0.0.1-SNAPSHOT">
+        <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
+                        
+        
+        <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="java-output-path" value="/WebTest/build/classes"/>
+                
+        <property name="context-root" value="WebTest"/>
+                    
+    
+    </wb-module>
+        
+
 </project-modules>
 
WebTest/.settings/org.eclipse.wst.validation.prefs (added)
+++ WebTest/.settings/org.eclipse.wst.validation.prefs
@@ -0,0 +1,2 @@
+disabled=06target
+eclipse.preferences.version=1
 
WebTest/pom.xml (added)
+++ WebTest/pom.xml
@@ -0,0 +1,48 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>WebTest</groupId>
+  <artifactId>WebTest</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>war</packaging>
+  <dependencies>
+	<dependency>
+	    <groupId>mysql</groupId>
+	    <artifactId>mysql-connector-java</artifactId>
+	    <version>5.1.49</version>
+	</dependency>
+	<dependency>
+	    <groupId>org.junit.jupiter</groupId>
+	    <artifactId>junit-jupiter-api</artifactId>
+	    <version>5.7.2</version>
+	    <scope>test</scope>
+	</dependency>
+	<dependency>
+	    <groupId>javax.servlet</groupId>
+	    <artifactId>jstl</artifactId>
+	    <version>1.2</version>
+	</dependency>
+	<dependency>
+	    <groupId>org.apache.taglibs</groupId>
+	    <artifactId>taglibs-standard-impl</artifactId>
+	    <version>1.2.5</version>
+	    <scope>runtime</scope>
+	</dependency>
+	
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.8.1</version>
+        <configuration>
+          <source>1.8</source>
+          <target>1.8</target>
+        </configuration>
+      </plugin>
+      <plugin>
+        <artifactId>maven-war-plugin</artifactId>
+        <version>3.2.3</version>
+      </plugin>
+    </plugins>
+  </build>
+</project>(No newline at end of file)
 
WebTest/src/main/java/config/Database.java (added)
+++ WebTest/src/main/java/config/Database.java
@@ -0,0 +1,28 @@
+package config;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+
+public class Database {
+
+	private static String dbHost = "db.asin21.com";
+	private static String dbUser = "demo07";
+	private static String dbPass = "demo07!";
+	private static String dbName = "db_demo07";
+
+	private static String dbDriver = "com.mysql.jdbc.Driver";
+	private static String dbUrl = "jdbc:mysql://" + dbHost + "/" + dbName;
+	
+	public static Connection getConnection() {
+		
+		try {
+			Class.forName(dbDriver);
+			return DriverManager.getConnection(dbUrl, dbUser, dbPass);
+			
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+			
+		}
+	}
+}
 
WebTest/src/main/java/config/EncodingFiter.java (added)
+++ WebTest/src/main/java/config/EncodingFiter.java
@@ -0,0 +1,74 @@
+package config;
+
+import java.io.IOException;
+import java.sql.Connection;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.annotation.WebFilter;
+
+/**
+ * Servlet Filter implementation class EncordingFiter
+ */
+@WebFilter("/*")
+public class EncodingFiter implements Filter {
+	
+	final private String charset = "utf-8";
+
+    /**
+     * Default constructor. 
+     */
+    public EncodingFiter() {
+    	
+        System.out.println("Fiter Created!");
+        
+    }
+
+	/**
+	 * @see Filter#destroy()
+	 */
+	public void destroy() {
+		
+		System.out.println("System Down!");
+		
+	}
+
+	/**
+	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
+	 */
+	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+
+		System.out.println("서블릿 요청이 있을 때마다 실행");		
+		
+		request.setCharacterEncoding(charset);
+		response.setContentType("text/html; charset=utf-8");
+		response.setCharacterEncoding(charset);
+		
+		chain.doFilter(request, response);
+	}
+
+	/**
+	 * @see Filter#init(FilterConfig)
+	 */
+	public void init(FilterConfig fConfig) throws ServletException {
+		
+		System.out.println("system Start");
+		Connection conn = Database.getConnection();
+		if(conn != null){
+			System.out.println(conn + " : DB Connention Success!");
+		}else {
+			System.out.println("DB Connention Fail!!");
+		}	
+	}
+}
+
+
+
+
+
+
+
 
WebTest/src/main/java/control/MemberServlet.java (added)
+++ WebTest/src/main/java/control/MemberServlet.java
@@ -0,0 +1,73 @@
+package control;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.RequestDispatcher;
+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 model.MemberDAO;
+import model.MemberDTO;
+
+/**
+ * Servlet implementation class MemberServlet
+ */
+@WebServlet("/Member/*")
+public class MemberServlet extends HttpServlet {
+	private static final long serialVersionUID = 1L;
+       
+    /**
+     * @see HttpServlet#HttpServlet()
+     */
+    public MemberServlet() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+	/**
+	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		
+		String url = request.getRequestURL().toString();
+		String viewPage = null;
+		
+		MemberDAO dao = new MemberDAO();
+		
+		if(url.indexOf("list.do") != -1) {
+			List<MemberDTO> list = dao.MemberList();
+			Map<String, Object> map = new HashMap<String, Object>();
+			
+			map.put("list", list);
+			map.put("count", list.size());
+			System.out.println(map);
+			request.setAttribute("map", map);
+			
+			viewPage = "/view/list.jsp";
+			RequestDispatcher goUrl = request.getRequestDispatcher(viewPage);
+			goUrl.forward(request, response);
+		}else {
+			
+		}
+		
+	}
+
+	/**
+	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		// TODO Auto-generated method stub
+		doGet(request, response);
+	}
+
+}
+
+
+
+
 
WebTest/src/main/java/model/MemberDAO.java (added)
+++ WebTest/src/main/java/model/MemberDAO.java
@@ -0,0 +1,70 @@
+package model;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.List;
+
+import config.Database;
+
+public class MemberDAO {
+
+	public List<MemberDTO> MemberList(){
+		
+		List<MemberDTO> list = new ArrayList<MemberDTO>();
+		
+		Connection conn = null;
+		PreparedStatement pstmt = null;
+		ResultSet rs = null;
+		
+		String query = "select * from user";
+		
+		try {
+			conn = Database.getConnection();
+			pstmt = conn.prepareStatement(query);
+			rs = pstmt.executeQuery();
+			
+			while(rs.next()) {
+				int no = rs.getInt("no");
+				String name = rs.getString("name");
+				int age = rs.getInt("age");
+				String role = rs.getString("role");
+				
+				MemberDTO dto = new MemberDTO();
+				
+				dto.setNo(no);
+				dto.setName(name);
+				dto.setAge(age);
+				dto.setRole(role);
+				
+				list.add(dto);
+			}
+			return list;
+			
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		}finally {
+			try {
+				if(rs != null) {
+					rs.close();
+				}
+				if(pstmt != null) {
+					pstmt.close();
+				}
+				if(conn != null) {
+					conn.close();
+				}
+			} catch (Exception e2) {
+				e2.printStackTrace();
+			}
+		}
+	}
+}
+
+
+
+
+
+
 
WebTest/src/main/java/model/MemberDTO.java (added)
+++ WebTest/src/main/java/model/MemberDTO.java
@@ -0,0 +1,42 @@
+package model;
+
+public class MemberDTO {
+
+	private int no;
+	private String name;
+	private int age;
+	public int getNo() {
+		return no;
+	}
+	public MemberDTO() {
+		super();
+	}
+	
+	@Override
+	public String toString() {
+		return "MemberDTO [no=" + no + ", name=" + name + ", age=" + age + ", role=" + role + "]\n";
+	}
+	public void setNo(int no) {
+		this.no = no;
+	}
+	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 getRole() {
+		return role;
+	}
+	public void setRole(String role) {
+		this.role = role;
+	}
+	private String role;
+	
+}
WebTest/src/main/java/servlet/Main.java
--- WebTest/src/main/java/servlet/Main.java
+++ WebTest/src/main/java/servlet/Main.java
@@ -33,11 +33,13 @@
 		response.setCharacterEncoding("UTF-8");
 		response.setContentType("text/html; charset=utf-8");
 		
+		String title = getInitParameter("title");
+		
 		PrintWriter out = response.getWriter();
 		out.println("<html lang='ko'>");
 		out.println("<head>");
 		out.println("<meta charset='UTF-8'>");
-		out.println("<title>Main Page</title>");
+		out.println("<title>" + title + "</title>");
 		out.println("</head>");
 		out.println("<body>");
 		out.println("<h3>This is Main Page</h3>");
WebTest/src/main/java/servlet/MyServlet.java
--- WebTest/src/main/java/servlet/MyServlet.java
+++ WebTest/src/main/java/servlet/MyServlet.java
@@ -29,19 +29,19 @@
 	 */
 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 		
-		request.setCharacterEncoding("UTF-8"); //post method korean
-		response.setCharacterEncoding("UTF-8"); //get method korean
-		response.setContentType("text/html; charset=utf-8");  //html contents korean
+//		request.setCharacterEncoding("UTF-8"); //post method korean
+//		response.setCharacterEncoding("UTF-8"); //get method korean
+//		response.setContentType("text/html; charset=utf-8");  //html contents korean
 		
 //		response.getWriter().append("Served at: ").append(request.getContextPath());
 		PrintWriter out = response.getWriter();
 		out.println("<html lang='ko'>");
 		out.println("<head>");
-		out.println("<meta charset='UTF-8'>");
+//		out.println("<meta charset='UTF-8'>");
 		out.println("<title>Servlet Test</title>");
 		out.println("</head>");
 		out.println("<body>");
-		out.println("<h3>MyServlet Test</h3>");
+		out.println("<h3>MyServlet Test 한글</h3>");
 		out.println("</body>");
 		out.println("</html>");
 		
WebTest/src/main/webapp/WEB-INF/web.xml
--- WebTest/src/main/webapp/WEB-INF/web.xml
+++ WebTest/src/main/webapp/WEB-INF/web.xml
@@ -9,9 +9,17 @@
     <welcome-file>default.htm</welcome-file>
     <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
+  <context-param>
+  	<param-name>global</param-name>
+  	<param-value>My WebSite</param-value>
+  </context-param>
   <servlet>
 	<servlet-name>main</servlet-name>
 	<servlet-class>servlet.Main</servlet-class>  
+	<init-param>
+		<param-name>title</param-name>
+		<param-value>Main Page</param-value>
+	</init-param>
   </servlet>
   
   <servlet>
@@ -21,6 +29,10 @@
   		<param-name>name</param-name>
   		<param-value>홍길동</param-value>
   	</init-param>
+  	<init-param>
+  		<param-name>age</param-name>
+  		<param-value>10</param-value>
+  	</init-param>
   </servlet>
   
   <servlet>
WebTest/src/main/webapp/test.jsp
--- WebTest/src/main/webapp/test.jsp
+++ WebTest/src/main/webapp/test.jsp
@@ -16,7 +16,14 @@
 	<h3><%=title %></h3>
 	<h4><%=title2 %></h4>
 	<p>
-		<%=getInitParameter("name") %>
+		<%=getInitParameter("name") %>의 나이는
+		<%=getInitParameter("age") %> 살 입니다.
 	</p>
+	<%
+		pageContext.setAttribute("iam", "Test 1");
+	
+		out.println(pageContext.getAttribute("iam"));
+	%>
+	<H3><%=application.getInitParameter("global") %></H3>
 </body>
 </html>
(No newline at end of file)
WebTest/src/main/webapp/test2.jsp
--- WebTest/src/main/webapp/test2.jsp
+++ WebTest/src/main/webapp/test2.jsp
@@ -6,7 +6,8 @@
 <meta charset="UTF-8">
 <title>Insert title here</title>
 </head>
-<body>	
+<body>
 	<%=getInitParameter("name") %>
-</body>	
+	<h4><%=application.getInitParameter("global") %></h4>
+</body>
 </html>
(No newline at end of file)
 
WebTest/src/main/webapp/view/list.jsp (added)
+++ WebTest/src/main/webapp/view/list.jsp
@@ -0,0 +1,78 @@
+<%@page import="model.MemberDTO"%>
+<%@page import="java.util.List"%>
+<%@page import="java.util.Map"%>
+<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+    pageEncoding="UTF-8"%>
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8">
+<title>Insert title here</title>
+</head>
+<body>
+<%
+	Map<String, Object> map = (Map<String, Object>) request.getAttribute("map");
+	List<MemberDTO> list = (List<MemberDTO>) map.get("list");
+	int count = (int) map.get("count");
+%>
+	<table border="1">
+		<thead>
+			<tr>
+				<th>번호</th>
+				<th>이름</th>
+				<th>나이</th>
+				<th>직위</th>
+			</tr>
+		</thead>
+		<% for(MemberDTO mb : list){ %>
+		<tr>
+			<td><%=mb.getNo() %></td>
+			<td><%=mb.getName() %></td>
+			<td><%=mb.getAge() %></td>
+			<td><%=mb.getRole() %></td>
+		</tr>
+		<% } %>
+	</table>
+	<br />
+	<br />
+	<br />
+	<table border="1">
+	<thead>
+			<tr>
+				<th>번호</th>
+				<th>이름</th>
+				<th>나이</th>
+				<th>직위</th>
+			</tr>
+		</thead>
+		<c:forEach var="row" items="${map.list}">
+		<tr>
+			<td>${row.no}</td>
+			<td>${row.name}</td>
+			<td>${row.age}</td>
+			<td>${row.role}</td>
+		</tr>
+		</c:forEach>
+	</table>
+</body>
+</html>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 
WebTest/src/test/java/exam/dbTest.java (added)
+++ WebTest/src/test/java/exam/dbTest.java
@@ -0,0 +1,59 @@
+package exam;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+import org.junit.jupiter.api.Test;
+
+class dbTest {
+
+	@Test
+	void test() {
+//		fail("Not yet implemented");
+		assertTrue(1==1); //true
+		assertFalse(1==2); //false
+	}
+	
+	@Test
+	void classForNameTest() {
+		try {
+			Class.forName("com.mysql.jdbc.Driver");
+		} catch (Exception e) {
+			e.printStackTrace();
+			fail("Driver Loading Error!!");
+		}
+	}
+	
+	@Test
+	void databaseTest() {
+		try {
+			Connection conn = DriverManager.getConnection("jdbc:mysql://db.asin21.com/db_demo07", "demo07", "demo07!");
+			PreparedStatement pstmt = conn.prepareStatement("select * from user");
+			ResultSet rs = pstmt.executeQuery();
+			while(rs.next()) {
+				System.out.print(rs.getInt("no") + "\t");
+				System.out.print(rs.getString("name") + "\t");
+				System.out.print(rs.getString("age") + "\t");
+				System.out.println(rs.getString("role") + "\t");
+			}
+			
+		} catch (Exception e) {
+			e.printStackTrace();
+			fail("Fail!! : Database Connention error");
+		}
+	}
+		
+}
+
+
+
+
+
+
+
+
+
Add a comment
List