모델1방식
JSP(.jsp)
1. JAVA 2.HTML 3.HTML+JAVA
혼자서 모든 걸 해야하기 때문에 개발 시간이 오래 걸리고 업무속도가 느리다. 소규모 프로젝트에 적합
모델2방식
Model(.java) ==> [Model단 DAO, VO(DTO)]
+
Java(.java)
Servlet ==> [Controller단(Servlet)]
+
JSP(.jsp) ==> [View단 .jsp]
개발시간 단축된다. 대규모 프로젝트에 적합
HashMap 사용하여 전체 회원목록 조회
-- Folder(begin.chap07.oracle.controller)에 Servlet(MemberListCtrl2) 생성
begin.chap07.oracle.controller MemberListCtrl2.java excute()
InterMemberDAO2 dao = new MemberDAO2();
try {
List<HashMap<String, String>> memberList = dao.selectMemberAll();
request.setAttribute("memberList", memberList);
RequestDispatcher dispatcher = request.getRequestDispatcher("/begin/chap07Oracle/MemberList.jsp");
dispatcher.forward(request, response);
} catch(SQLException e) {
String errorMsg = e.getMessage();
request.setAttribute("errorMsg", errorMsg);
RequestDispatcher dispatcher = request.getRequestDispatcher("/begin/chap07Oracle/sqlError2.jsp");
dispatcher.forward(request, response);
e.printStackTrace();
} catch(Exception e) {
String errorMsg = e.getMessage();
request.setAttribute("errorMsg", errorMsg);
RequestDispatcher dispatcher = request.getRequestDispatcher("/begin/chap07Oracle/anotherError2.jsp");
dispatcher.forward(request, response);
e.printStackTrace();
}
begin.chap07.oracle.model InterMemberDAO2
// 회원목록조회(select)
List<HashMap<String, String>> selectMemberAll() throws SQLException;
begin.chap07.oracle.model MemberDAO2
@Override
public List<HashMap<String, String>> selectMemberAll()
throws SQLException {
List<HashMap<String, String>> memberList = null;
try {
conn = MyDBConnection.getConn();
String sql = " select userid, name, email,\n" +
" to_char(registerday, 'yyyy-mm-dd hh24:mi:ss') AS registerday\n"+
" from myweb_member\n"+
" order by registerday desc ";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
int cnt = 0;
while(rs.next()) {
if(cnt == 1)
memberList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("userid", rs.getString("userid"));
map.put("name", rs.getString("name"));
map.put("email", rs.getString("email"));
map.put("registerday", rs.getString("registerday"));
memberList.add(map);
}
} finally {
close();
}
return memberList;
}
-- Folder(chap07Oracle) 에 JSP File(MemberList) 생성
chap07Oracle MemberList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%
Object obj = request.getAttribute("memberList");
List<HashMap<String, String>> memberList = null;
if(obj != null)
memberList = (List<HashMap<String, String>>)obj;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원목록 보여주기</title>
<style type="text/css">
div#container {
width: 95%;
margin: 0 auto;
}
table {
width: 90%;
border: solid 1px gray;
border-collapse: collapse;
margin-top: 30px;
text-align: center;
}
th, td {
border: solid 1px gray;
border-collapse: collapse;
}
tbody tr:hover {
background-color: #ccc;
cursor: pointer;
}
button {
margin-top: 30px;
margin-right: 180px;
float: right;
}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<div id="container">
<h2>회원목록 보기</h2>
<table>
<thead>
<tr>
<th>번호</th>
<th>아이디</th>
<th>회원명</th>
<th>이메일</th>
<th>가입일자</th>
</tr>
</thead>
<tbody>
<%
if(memberList != null) {
for(int i=0; i<memberList.size(); i++){ %>
<tr>
<td><%= i+1%></td>
<td><%= memberList.get(i).get("userid") %></td>
<td><%= memberList.get(i).get("name") %></td>
<td><%= memberList.get(i).get("email") %></td>
<td><%= memberList.get(i).get("registerday") %></td>
</tr>
<% }
}
else { %>
<tr>
<td colspan="5">존재하는 회원이 없습니다.</td>
</tr>
<% } %>
</tbody>
</table>
<button type="button" onclick="">회원가입</button>
</div>
</body>
</html>
HashMap 사용하여 상세 회원목록 조회
begin.chap07.oracle.controller DetailMemberInfoCtril2 excute()
InterMemberDAO2 dao = new MemberDAO2();
try {
String userid = request.getParameter("userid");
HashMap<String, String> map = dao.selectMemberOne(userid);
request.setAttribute("map", map);
RequestDispatcher dispatcher = request.getRequestDispatcher("/begin/chap07Oracle/memberDetail2.jsp");
dispatcher.forward(request, response);
} catch(SQLException e) {
String errorMsg = e.getMessage();
request.setAttribute("errorMsg", errorMsg);
RequestDispatcher dispatcher = request.getRequestDispatcher("/begin/chap07Oracle/sqlError2.jsp");
dispatcher.forward(request, response);
e.printStackTrace();
} catch(Exception e) {
String errorMsg = e.getMessage();
request.setAttribute("errorMsg", errorMsg);
RequestDispatcher dispatcher = request.getRequestDispatcher("/begin/chap07Oracle/anotherError2.jsp");
dispatcher.forward(request, response);
e.printStackTrace();
}
begin.chap07.oracle.model InterDAO2
HashMap<String, String> selectMemberOne(String userid) throws SQLException;
begin.chap07.oracle.model DAO2
@Override
public HashMap<String, String> selectMemberOne(String userid) throws SQLException {
HashMap<String, String> map = null;
try {
conn = MyDBConnection.getConn();
String sql = "select userid, passwd, name, email, tel, jubun,\n"+
" case when substr(jubun,7,1) in ('1','3') then '남자' else '여자' end AS gender,\n"+
" extract(year from sysdate) - (substr(jubun, 1,2) + (case when substr(jubun, 7, 1) in ('1','2') then 1900 else 2000 end)) + 1 AS age,\n"+
" school, interest,\n" +
" to_char(registerday, 'yyyy-mm-dd hh24:mi:ss') AS registerday\n" +
" from myweb_member\n"+
" where userid = ? \n" +
" order by registerday desc ";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userid);
rs = pstmt.executeQuery();
if(rs.next()) {
map = new HashMap<String, String>();
map.put("userid", rs.getString("userid"));
map.put("passwd", rs.getString("passwd"));
map.put("name", rs.getString("name"));
map.put("email", rs.getString("email"));
map.put("tel", rs.getString("tel"));
map.put("jubun", rs.getString("jubun"));
map.put("gender", rs.getString("gender"));
map.put("age", rs.getString("age"));
map.put("school", rs.getString("school"));
map.put("interest", rs.getString("interest"));
map.put("registerday", rs.getString("registerday"));
}
} finally {
close();
}
return map;
}
cahp07Oracle memberDetail2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import= "java.util.*" %>
<%
Object obj = request.getAttribute("map");
HashMap<String, String> map = null;
if(obj != null)
map = (HashMap<String, String>)obj;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<title>회원상세정보 보여주기</title>
<style type="text/css">
ul {
list-style-type: square;
}
li {
line-height: 200%;
}
label {
display: inline-block;
width: 100px;
text-align: left;
color: navy;
font-size: 13pt;
}
span {
font-size: 12pt;
font-weight: bold;
}
div#container {
position: absolute;
left: 100px;
top: 20px;
}
</style>
</head>
<body>
<div id="container">
<h2>회원상세정보 조회 결과</h2>
<% if(map != null) { %>
<ul>
<li>
<label>회원아이디</label>
<span><%= map.get("userid")%></span>
</li>
<li>
<label>암호</label>
<span><%= map.get("passwd")%></span>
</li>
<li>
<label>회원명</label>
<span><%= map.get("name")%></span>
</li>
<li>
<label>이메일</label>
<span><%= map.get("email")%></span>
</li>
<li>
<label>전화번호</label>
<span><%= map.get("tel")%></span>
</li>
<li>
<label>주민번호</label>
<span><%= map.get("jubun")%></span>
</li>
<li>
<label>성별</label>
<span><%= map.get("gender")%></span>
</li>
<li>
<label>나이</label>
<span><%= map.get("age")%></span>
</li>
<li>
<label>학력</label>
<span><%= map.get("school")%></span>
</li>
<li>
<label>관심분야</label>
<span><%= map.get("interest")%></span>
</li>
<li>
<label>가입일자</label>
<span><%= map.get("registerday")%></span>
</li>
</ul>
<%} else { %>
<span style='color: red;'>존재하지 않는 회원입니다.</span>
<% } %>
<button type="button" onclick="javascript:location.href='memberList2.do'">회원목록</button>
</div>
</body>
</html>
chap07Oracle memberList.jsp javascript
function goMemberDetail(userid){
location.href="detailMemberInfo2.do?userid="+userid;
}
chap07Oracle memberList.jsp table 태그의 td 태그
<td onclick="goMemberDetail('<%= memberList.get(i).get("userid")%>');"><%= i+1%></td>
<td onclick="goMemberDetail('<%= memberList.get(i).get("userid")%>');"><%= memberList.get(i).get("userid") %></td>
<td onclick="goMemberDetail('<%= memberList.get(i).get("userid")%>');"><%= memberList.get(i).get("name") %></td>
<td onclick="goMemberDetail('<%= memberList.get(i).get("userid")%>');"><%= memberList.get(i).get("email") %></td>
<td onclick="goMemberDetail('<%= memberList.get(i).get("userid")%>');"><%= memberList.get(i).get("registerday") %></td>
Ⅰ. JSTL
https://tomcat.apache.org/taglibs/standard/
-- 위의 페이지에서 Standard 1.2.3
-- 아래 파일 3개 다운로드
-- 다운받은 파일 3개를 복사하여 WEB-INF 안에 붙여넣기
두 개의 수를 입력받아서 곱셈하기
-- Folder(chap08JSTL)에 JSP File(01Multyply, 01MultiplyEnd) 생성
chap08JSTL 01Multyply.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>두 개의 수를 입력받아서 곱셈하기</title>
<script type="text/javascript">
function goSubmit() {
var frm = document.multiplyFrm;
frm.action = "01MultiplyEnd.jsp";
frm.submit();
}
</script>
</head>
<body>
<form name="multiplyFrm">
첫 번째 수 : <input type="number" name="num1"/><br/>
두 번째 수 : <input type="number" name="num2"/><br/>
<button type="button" onclick="goSubmit();">확인</button>
<button type="reset">취소</button>
</form>
</body>
</html>
chap08JSTL 01MultiplyEnd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="no1" value="${param.num1}"/>
<c:set var="no2" value="${param.num2}"/>
<c:set var="result" value="${no1 * no2}"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>두 개의 수를 입력받아서 곱셈한 결과 출력하기</title>
</head>
<body>
${no1} 와 ${no2} 의 곱은 ? ${result} 입니다. <br><br/>
${param.num1} 와 ${param.num2} 의 곱은 ? ${result} 입니다. <br><br/>
</body>
</html>
1. If
입력받은 두 개의 문자열이 같은지 다른지 검사하기
-- Folder(chap08JSTL)에 JSP File(02IfTest, 02IfTestEnd) 생성
chap08JSTL 02IfTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>입력받은 두 개의 문자열이 같은지 다른지 검사하기</title>
<script type="text/javascript">
function goSubmit() {
var frm = document.myFrm;
frm.action = "02IfTestEnd.jsp";
frm.submit();
}
</script>
</head>
<body>
<form name="myFrm">
첫 번째 입력란 : <input type="text" name="first"/><br/>
두 번째 입력란 : <input type="text" name="second"/><br/>
<button type="button" onclick="goSubmit();">확인</button>
<button type="reset">취소</button>
</form>
</body>
</html>
chap08JSTL 02IfTestEnd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>입력받은 두 개의 문자열이 같은지 다른지 검사하기 결과</title>
</head>
<body>
첫 번째 문자열 : ${param.first}<br/>
두 번째 문자열 : ${param.second}<br/>
<c:if test="${param.first == param.second}">
${param.fisrt} 와 ${param.second} 은 같습니다.
</c:if>
<c:if test="${param.first != param.second}">
${param.first} 와 ${param.second} 은 같지 않습니다.
</c:if>
</body>
</html>
chap08JSTL 02IfTestEnd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>입력받은 두 개의 문자열이 같은지 다른지 검사하기 결과</title>
</head>
<body>
첫 번째 문자열 : ${param.first}<br/>
두 번째 문자열 : ${param.second}<br/>
<c:if test="${param.first eq param.second}">
${param.first} 와 ${param.second} 은 같습니다.
</c:if>
<c:if test="${param.first ne param.second}">
${param.first} 와 ${param.second} 은 같지 않습니다.
</c:if>
</body>
</html>
-- 결과는 위와 동일하다.
chap08JSTL 02IfTestEnd.jsp
<c:set var="personVO" value="홍길동"/>
<c:if test="${!empty personVO}">
personVO에는 정보가 들어있습니다.
</c:if>
<c:if test="${empty personVO}">
personVO에는 정보가 들어있지 않습니다.
</c:if>
<br/><br/>
<c:set var="productVO" value=""/>
<c:if test="${!empty productVO}">
productVO에는 정보가 들어있습니다.
</c:if>
<c:set var="productVO" value=""/>
<c:if test="${empty productVO}">
productVO에는 정보가 들어있지 않습니다.
</c:if>
2. Choose
-- Folder(chap08JSTL)에 JSP File(03ChooseTest, 03ChooseTestEnd) 생성
chap08JSTL 03ChooseTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>숫자를 입력받아서 1,3(남자) / 2,4(여자)로 나타내기</title>
<script type="text/javascript">
function goSubmit() {
var frm = document.myFrm;
frm.action = "03ChooseTestEnd.jsp";
frm.submit();
}
</script>
</head>
<body>
<form name="myFrm">
숫자입력 : <input type="text" name="num"/><br/>
<br/>
<button type="button" onclick="goSubmit();">확인</button>
<button type="reset">취소</button>
</form>
</body>
</html>
chap08JSTL 03ChooseTestEnd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>숫자를 입력받아서 1,3(남자) / 2,4(여자)로 나타내기 결과</title>
</head>
<body>
<c:choose>
<c:when test="${param.num eq '1'}">
남자입니다.<br/>
</c:when>
<c:when test="${param.num eq '3'}">
남자입니다.<br/>
</c:when>
<c:when test="${param.num eq '2'}">
여자입니다.<br/>
</c:when>
<c:when test="${param.num eq '4'}">
여자입니다.<br/>
</c:when>
<c:otherwise>
1, 2, 3, 4 중 하나만 입력하세요.<br/>
</c:otherwise>
</c:choose>
</body>
</html>
3. For each
-- Folder(chap08JSTL)에 JSP File(04ForEach) 생성
chap08JSTL 04ForEach.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>반복문 연습</title>
</head>
<body>
<c:forEach var="i" begin="1" end="6">
<h${i}>반복문 연습</h${i}>
</c:forEach>
</body>
</html>
-- Folder(chap08JSTL)에 JSP File(05ForEach2Data, 05ForEach2View) 생성
chap08JSTL 05ForEach2Data.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String[] nameArr = {"일순신","이순신","삼순신","사순신","오순신"};
request.setAttribute("nameArr", nameArr);
RequestDispatcher dispatcher = request.getRequestDispatcher("05ForEach2View.jsp");
dispatcher.forward(request, response);
%>
▷ JAVA
chap08JSTL 05ForEach2View.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Object obj = request.getAttribute("nameArr");
String[] nameArr = null;
if(obj != null) {
nameArr = (String[])obj;
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원명단 출력하기</title>
</head>
<body>
<h2>회원명단</h2>
<ol>
<%
if(nameArr != null) {
for(int i=0; i<nameArr.length; i++) {
%>
<li><%= nameArr[i] %></li>
<% }
}
else { %>
<li>등록된 회원이 없습니다.</li>
<% } %>
</ol>
</body>
</html>
▷ JSTL
-- Folder(chap08JSTL)에 JSP File(05ForEach2Data, 05ForEach2View) 복사하여 JSP File(06ForEach3Data, 06ForEach3View) 생성
chap08JSTL 06ForEach3Data.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String[] nameArr = {"일순신","이순신","삼순신","사순신","오순신"};
request.setAttribute("nameArr", nameArr);
RequestDispatcher dispatcher = request.getRequestDispatcher("06ForEach3View.jsp");
dispatcher.forward(request, response);
%>
chap08JSTL 06ForEach3View.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원명단 출력하기</title>
</head>
<body>
<h2>회원명단(JSTL을 사용한 것)</h2>
<c:if test="${!empty nameArr}">
<ol>
<c:forEach var="irum" items="${nameArr}">
<li>${irum}</li>
</c:forEach>
</ol>
</c:if>
<c:if test="${empty nameArr}">
회원명단이 존재하지 않습니다.
</c:if>
</body>
</html>